Created
April 22, 2016 09:04
-
-
Save southp/16c9fc2bea3ba265b9d430c20606d58d to your computer and use it in GitHub Desktop.
This file contains 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
"use strict"; | |
const toMs = ( beg, end ) => { | |
const sec = end[0] - beg[0]; | |
const ms = Math.floor( (end[1] - beg[1]) / 1000 ); | |
return sec * 1000 + ms; | |
}; | |
const SIZE = 1000000; | |
let arr = []; | |
let obj = {}; | |
for ( var i = 0; i < SIZE; ++i ) { | |
let val = Math.floor( Math.random() * 256 ); | |
arr.push( val ); | |
obj[ "haha" + i ] = val; | |
} | |
let prev = process.hrtime(); | |
let sum = 0; | |
for ( var i = 0; i < SIZE; ++i ) { | |
let noop = "haha" + i; | |
sum += arr[ i ]; | |
} | |
let t1 = toMs( prev, process.hrtime() ); | |
console.log( "Time cost: ", t1, "ms", sum ); | |
arr.sort(); | |
prev = process.hrtime(); | |
sum = 0; | |
for ( var i = 0; i < SIZE; ++i ) { | |
sum += obj[ "haha" + i ]; | |
} | |
let t2 = toMs( prev, process.hrtime() ); | |
console.log( "Time cost: ", t2, "ms", sum ); | |
console.log( "Ratio t2/t1: ", t2 / t1 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment