I’ve introduced a new method for loading SyntaxHighlighter onto a web page. The goal was simply to make it easier to integrate SyntaxHighlighter into a web site. So, I was pleasantly surprised to find that the new method often loads faster than the traditional method.
Arrays can present an unexpected performance bottleneck in JavaScript. Here I show that array performance is influenced by some surprising details of the array and how it’s used.
The JavaScript interpreters in the most popular browsers distinguish between sparse and dense arrays. Hence, you may get more “array-like” performance if you initialize an array before using it. But this trades memory for speed, so it may not always be the best choice.
It may well be that for better JavaScript performance, large arrays should be avoided where possible. That seems fairly clear in this case, where an already fast implementation of the missing String.trim() method was made even faster by using much smaller lookup table arrays.