# HG changeset patch # User dellsystem # Date 1350094384 14400 # Node ID 2a2078bd334c862c344154ad1a33068fe94b37bb # Parent 3c4dc3b15f3b1043f216ff31640926a52be2b7a6 Optimise Javascript for adding line numbers Decreased the number of DOM manipulations and changed the $.each() into a regular for loop. It was taking an unreasonable amount of time for code snippets several hundred lines long. diff --git a/static/js/agora.js b/static/js/agora.js --- a/static/js/agora.js +++ b/static/js/agora.js @@ -43,14 +43,20 @@ // Add in the line numbers (no JS fallback unfortunately) if ($('.snippet').length) { var counter = 1; + var lineNumbers = []; - $('.code-lines').children().each(function () { + var lines = $('.code-lines').children(); + + for (var i = 0, numLines = lines.length; i < numLines; i++) { + var line = lines[i]; // Set the top offset to be the same as that of the line - var div = '

' + + var div = '

' + '' + counter + '

'; counter++; - $('.line-counters').append(div); - }); + lineNumbers.push(div); + } + + $('.line-counters').append(lineNumbers.join('')); } // Highlight the code when the link is clicked