Created
September 9, 2011 16:43
-
-
Save scheakur/1206699 to your computer and use it in GitHub Desktop.
Check performance of detecting arrays in Rhino
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
// Original is http://d.hatena.ne.jp/uupaa/20090116/1232051707 | |
var array = []; | |
var notarray = {}; | |
(function () { | |
var S = new Date*1; | |
var toString = Object.prototype.toString; | |
for (var i = 0; i < 5000000; i++) { ( toString.call(array) === "[object Array]" ) } | |
print('Array#Object.prototype.toString.call : ' + (new Date-S)); | |
}()); | |
(function () { | |
var S = new Date*1; | |
var toString = Object.prototype.toString; | |
for (var i = 0; i < 5000000; i++) { ( toString.call(notarray) === "[object Array]" ) } | |
print('notArray#Object.prototype.toString.call : ' + (new Date-S)); | |
}()); | |
(function () { | |
var S = new Date*1; | |
for (var i = 0; i < 5000000; i++) { ( "length" in array ) } | |
print('"length" in array : ' + (new Date-S)); | |
}()); | |
(function () { | |
var S = new Date*1; | |
for (var i = 0; i < 5000000; i++) { ( "length" in notarray ) } | |
print('"length" in notarray : ' + (new Date-S)); | |
}()); | |
(function () { | |
var S = new Date*1; | |
for (var i = 0; i < 5000000; i++) { (array instanceof Array) } | |
print('Array#instanceof : ' + (new Date-S)); | |
}()); | |
(function () { | |
var S = new Date*1; | |
for (var i = 0; i < 5000000; i++) { (notarray instanceof Array) } | |
print('notArray#instanceof : ' + (new Date-S)); | |
}()); | |
(function () { | |
var S = new Date*1; | |
for (var i = 0; i < 5000000; i++) { (Array.isArray(array)) } | |
print('Array#Array.isArray : ' + (new Date-S)); | |
}()); | |
(function () { | |
var S = new Date*1; | |
for (var i = 0; i < 5000000; i++) { (Array.isArray(notarray)) } | |
print('notArray#Array.isArray : ' + (new Date-S)); | |
}()); |
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
Result with Rhino 1.7R3 | |
TEST | time [ms] | |
-----------------------------------------+------------ | |
Array#Object.prototype.toString.call | 968 | |
notArray#Object.prototype.toString.call | 816 | |
"length" in array | 180 | |
"length" in notarray | 294 | |
Array#instanceof | 321 | |
notArray#instanceof | 344 | |
Array#Array.isArray | 361 | |
notArray#Array.isArray | 371 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment