Skip to content

Instantly share code, notes, and snippets.

@chilts
Created August 26, 2013 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilts/6345907 to your computer and use it in GitHub Desktop.
Save chilts/6345907 to your computer and use it in GitHub Desktop.
Gist to compare two ways of creating the string for flake (https://npmjs.org/package/flake) IDs. I suspect the construction of the array far outweighs the construction of the string.
var t1, t2, t3;
var i;
const TOP = 1000000000;
var tsHex = '013c798f75ad';
var counterHex = '0000';
var pidHex = '1154';
var macHex = '984be1b8b104';
t1 = Date.now();
for (i=0; i< TOP; i++) {
tsHex + '-' + counterHex + '-' + pidHex + '-' + macHex;
}
t2 = Date.now();
for (i=0; i< TOP; i++) {
[tsHex, counterHex, pidHex, macHex].join('-');
}
t3 = Date.now();
// about 85ms
console.log('1=' + (t2-t1) + 'ms');
// about 53000ms (53s)!!
console.log('2=' + (t3-t2) + 'ms');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment