Skip to content

Instantly share code, notes, and snippets.

@fosron
Last active February 21, 2017 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fosron/82245868e75ba5762f35c130dd641022 to your computer and use it in GitHub Desktop.
Save fosron/82245868e75ba5762f35c130dd641022 to your computer and use it in GitHub Desktop.
Micro-optimisations for JS
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