Created
November 16, 2008 18:19
-
-
Save kangax/25522 to your computer and use it in GitHub Desktop.
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
| 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