Skip to content

Instantly share code, notes, and snippets.

@joliss
Last active March 14, 2017 17:08
Show Gist options
  • Save joliss/4171cf62ab15c8bf7eb6e4e81eeb8bdc to your computer and use it in GitHub Desktop.
Save joliss/4171cf62ab15c8bf7eb6e4e81eeb8bdc to your computer and use it in GitHub Desktop.
#!/usr/bin/node
const N = 1000000;
// 130 ms
(function() {
console.time('+=');
let a = '';
for (let i = 0; i < N; i++) {
a += 'x';
}
console.timeEnd('+=');
})();
// 70 ms
(function() {
console.time('= +');
let a = '';
for (let i = 0; i < N; i++) {
a = a + 'x';
}
console.timeEnd('= +');
})();
@fhinkel
Copy link

fhinkel commented Mar 14, 2017

V8 5.9 with turbofan. The generated optimized code looks the same to me. Seems like the first function is always the slower one. So += is perfectly fine to use in the future. You can download Node with newest V8 for Ubuntu from here.

~/code/JoLissExample$ ../../Downloads/bin/node -e "console.log(process.versions.v8)"
5.9.0 (candidate)
~/code/JoLissExample$ ../../Downloads/bin/node  loop.js
= +: 108.032ms
+=: 66.177ms
= +: 78.599ms

Old V8 (Node 7) with crankshaft:

~/code/JoLissExample$ node --version
v7.7.2
~/code/JoLissExample$ node -e "console.log(process.versions.v8)"
5.5.372.41
~/code/JoLissExample$ node loop.js
= +: 73.538ms
+=: 103.824ms
= +: 28.473ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment