Skip to content

Instantly share code, notes, and snippets.

@syoichi
Forked from kyo-ago/gist:1619107
Created January 16, 2012 10:47
Show Gist options
  • Save syoichi/1620183 to your computer and use it in GitHub Desktop.
Save syoichi/1620183 to your computer and use it in GitHub Desktop.
Document.getElementByIdのnative/wrapper/aliasの実行速度をChrome 16.0.912.75上でconsole.timeにより計測する
/*jslint devel: true, browser: true, maxerr: 50, maxlen: 80, indent: 4 */
// Edition 2012-01-13
// Chrome 16.0.912.75(V8 3.6.6.15, WebKit 535.7(@103989)) on Windows 7 Home Premium SP1 64bit
(function executeTest(doc, LOOP) {
'use strict';
var body, iterator1, iterator2, iterator3, iterator4, $id, getId, id,
assign1, assign2, assign3, assign4;
body = doc.body;
iterator1 = iterator2 = iterator3 = LOOP;
$id = function $id(elmId) {
return doc.getElementById(elmId);
};
getId = doc.getElementById;
id = doc.getElementById.bind(doc);
body.parentNode.replaceChild(body.cloneNode(false), body);
body = doc.body;
body.id = 'test';
console.time('Document.getElementById');
while (iterator1) {
iterator1 -= 1;
assign1 = doc.getElementById('test');
}
console.timeEnd('Document.getElementById');
console.time('Document.getElementById wrapper');
while (iterator2) {
iterator2 -= 1;
assign2 = $id('test');
}
console.timeEnd('Document.getElementById wrapper');
console.time('Document.getElementById + Function.call');
while (iterator3) {
iterator3 -= 1;
assign3 = getId.call(doc, 'test');
}
console.timeEnd('Document.getElementById + Function.call');
console.time('Document.getElementById + Function.bind');
while (iterator4) {
iterator4 -= 1;
assign4 = id('test');
}
console.timeEnd('Document.getElementById + Function.bind');
}(document, 1000000));
/*
Document.getElementById: 88ms
Document.getElementById wrapper: 149ms
Document.getElementById + Function.call: 147ms
Document.getElementById + Function.bind: 503ms
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment