Skip to content

Instantly share code, notes, and snippets.

@kangax
Created November 16, 2008 18:19
Show Gist options
  • Select an option

  • Save kangax/25522 to your computer and use it in GitHub Desktop.

Select an option

Save kangax/25522 to your computer and use it in GitHub Desktop.
var isNumber = (function(){
var toString = Object.prototype.toString;
return function(o) {
return toString.call(o) === '[object Number]';
}
})();
function isNumber2(o) {
try {
return typeof (o && o.valueOf()) == 'number';
} catch(e) {}
return false;
}
console.time('class');
for (var i=0; i<10000; i++) { isNumber(10) }
console.timeEnd('class');
console.time('valueof');
for (var i=0; i<10000; i++) { isNumber2(10) }
console.timeEnd('valueof');
// class: 41ms
// valueof: 18ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment