Skip to content

Instantly share code, notes, and snippets.

@shrunyan
Last active December 21, 2015 18:19
Show Gist options
  • Save shrunyan/6346254 to your computer and use it in GitHub Desktop.
Save shrunyan/6346254 to your computer and use it in GitHub Desktop.
Testing performance of different for loop variations in JavaScript. Creating 100,000 anchor link elements.
/**
* v0.1 - removing anchor tag DOM insertion; not neccessary for loop test.
*/
console.time('Multi-variable:');
for (var i = 0, k = 100000; i < k; i++) {
var a = document.createElement('A');
a.href = 'http://www.google.com';
a.innerHTML = 'Link';
};
console.timeEnd('Multi-variable:');
console.time('Variable subtraction:');
for (var i = 100000; i--;) {
var a = document.createElement('A');
a.href = 'http://www.google.com';
a.innerHTML = 'Link';
};
console.timeEnd('Variable subtraction:');
console.time('Standard:');
for (var i = 0; i < 100000; i++) {
var a = document.createElement('A');
a.href = 'http://www.google.com';
a.innerHTML = 'Link';
};
console.timeEnd('Standard:');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment