Skip to content

Instantly share code, notes, and snippets.

@JosephPecoraro
Created May 9, 2010 22:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosephPecoraro/395472 to your computer and use it in GitHub Desktop.
Save JosephPecoraro/395472 to your computer and use it in GitHub Desktop.
// So this will run in a browser as well as a shell.
if (this.window) {
window.print = function(s) { console.log(s); }
}
var TOTAL = 1e5;
var row = 0;
var data = {
rows: [{ a: "hi", b: 1, c: true, d: "wah!" }]
};
print('expected: ' + TOTAL*3);
// Test without caching property lookups
(function() {
var start = Date.now();
var count = 0;
for (var i=0; i<TOTAL; ++i)
for (var item in data['rows'][row])
if (typeof data['rows'][row][item] === 'string' || typeof data['rows'][row][item] === 'number')
count++;
var end = Date.now();
print('count: ' + count);
print(end-start+'ms');
})();
// Test with caching property lookups
(function() {
var start = Date.now();
var count = 0;
for (var i=0; i<TOTAL; ++i) {
var theRow = data.rows[row];
for (var itemName in theRow) {
var item = theRow[itemName];
if (typeof item === 'string' || typeof item === 'number')
count++;
}
}
var end = Date.now();
print('count: ' + count);
print(end-start+'ms');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment