Created
October 23, 2010 23:09
-
-
Save akidee/642804 to your computer and use it in GitHub Desktop.
Duck typing vs. instanceof ... script
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 repeat = 100000; | |
// 1a) Duck typing - true | |
var t = +new Date(), | |
obj = [1,2,3]; | |
for (var i = 0; i < repeat; i++){!!(obj && obj.concat && obj.unshift && !obj.callee);} | |
console.log(+new Date() - t); | |
// 1b) Duck typing - false | |
t = +new Date(), | |
obj = {}; | |
for (var i = 0; i < repeat; i++){!!(obj && obj.concat && obj.unshift && !obj.callee);} | |
console.log(+new Date() - t); | |
// 2a) instanceof - true | |
t = +new Date(), | |
obj = [1,2,3]; | |
for (var i = 0; i < repeat; i++){obj instanceof Array;} | |
console.log(+new Date() - t); | |
// 2b) instanceof - false | |
t = +new Date(), | |
obj = {}; | |
for (var i = 0; i < repeat; i++){obj instanceof Array;} | |
console.log(+new Date() - t); |
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
FF 3.6b3 | |
285 | |
200 | |
191 | |
192 | |
GC 7.0 | |
245 | |
178 | |
184 | |
182 | |
Safari 5.0.2 | |
56 | |
37 | |
32 | |
32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment