Skip to content

Instantly share code, notes, and snippets.

@akidee
Created October 23, 2010 23:09
Show Gist options
  • Save akidee/642804 to your computer and use it in GitHub Desktop.
Save akidee/642804 to your computer and use it in GitHub Desktop.
Duck typing vs. instanceof ... script
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);
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