Skip to content

Instantly share code, notes, and snippets.

@scheakur
Created September 9, 2011 16:43
Show Gist options
  • Save scheakur/1206699 to your computer and use it in GitHub Desktop.
Save scheakur/1206699 to your computer and use it in GitHub Desktop.
Check performance of detecting arrays in Rhino
// 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));
}());
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