Last active
February 21, 2017 12:12
-
-
Save fosron/82245868e75ba5762f35c130dd641022 to your computer and use it in GitHub Desktop.
Micro-optimisations for JS
This file contains 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
var timer = function(name) { | |
var start = new Date(); | |
return { | |
stop: function() { | |
var end = new Date(); | |
var time = end.getTime() - start.getTime(); | |
console.log('Timer:', name, 'finished in', time, 'ms'); | |
} | |
} | |
}; | |
var t = timer('Moment'); for(var i=0; i < 10000; i++){ moment().year(); } t.stop(); | |
// Moment finished in 88 ms | |
var t = timer('Vanilla'); for(var i=0; i < 10000; i++){ new Date().getFullYear(); } t.stop(); | |
// Vanilla finished in 6 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment