Optimizations in a broad sense will involve simplifying code, precomputing results which are repeatedly reused, and organizing code so more results can be reused. From the standpoint of computer programming purity, optimizations should increase the simplicity, clarity and generality of a computer program. (See The Practice of Programming by Brian Kernighan and Rob Pike.)
Adding simplicity, clarity and generality is what these
optimizations will do. In fact, one of the optimizations adds support
for Unicode and multibyte characters such as
and still improves performance drastically
compared to the slower unoptimized version!
We analyze four optimizations in this article. More complicated optimizations or ones with bigger payoffs are listed after easy-to-use optimizations.
- Use local function variables
(82% improvement, 63 microseconds versus 359).
- Avoid references to objects or object properties
(41% improvement, 27.68 microseconds versus 47.297).
- Avoid adding short strings to long strings
(93% improvement, 4.608 microseconds versus 62.54).
- Use buffering to process data in optimal sizes
(96% improvement, 2.0 seconds versus 50.0).
Technique 1: Use local function variables
Using local function variables is simple. If you have code which uses variables repetitively, it's worthwhile to make the code into a function to take advantage of the higher performance of local function variables.
Global variables have slow performance because they live in a highly-populated
Not only are they stored along with many other
also distinguish between global variables and properties of objects that are in the current context.
Many objects in the current context can be referred to
by a variable name rather than as an object property,
alert() being synonymous with
window.alert(). The down side is this convenience slows
down code that uses global variables.
Sometimes global variables also have higher performance, like local function variables,
if you declare them
explicitly with the
var keyword. An example is
var d = document, instead of
d = document, but
it's not reliable. Mysterious behavior that works sometimes but not always is
a danger sign, and I feel more comfortable using local function variables.
optimizations like this, or who simply don't care. For example,
md5.js script, I had no intention of competing for the top position,
and yet the code has been unbeaten since 2003.
Let's figure out why local function variables make a difference by counting to a million, first without local function variables, and then with local function variables.
Counting to one million without local function variables
new Date() object returns the time difference in milliseconds
when it's subtracted from another
new Date() object, thus providing a
great way to time your scripts.
Counting to one million with local function variables
Code that gives us the results of the timing
The result is 359 milliseconds (thousandths of a second) when not using local function variables compared to 63 milliseconds when local function variables are used. This improvement is worth taking the extra time to convert code into a function.