Last active
December 21, 2015 18:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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