Last active
October 17, 2016 18:43
-
-
Save Salakar/6d7b84f7adf1f3bc62a754752a6e5d0e to your computer and use it in GitHub Desktop.
var vs let performance comparison in v8 - 'let' with --trace-deopt logs 'Unsupported let compound assignment'
This file contains hidden or 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
/** | |
Platform info: | |
Darwin 15.6.0 x64 | |
Node.JS 6.6.0 | |
V8 5.1.281.83 | |
Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz × 8 | |
var vs let | |
922,009,444 op/s » with var | |
19,823,034 op/s » with let | |
Suites: 1 | |
Benches: 2 | |
Elapsed: 5,900.32 ms | |
*/ | |
// benchmark below uses 'matcha' - feel free to adapt to which ever benchmark system you use. | |
console.log('\r\n'); | |
function withLet(str) { | |
let test = 'abcd' + 'efgh'; | |
test += str; | |
return test; | |
} | |
function withVar(str) { | |
var test = 'abcd' + 'efgh'; | |
test += str; | |
return test; | |
} | |
// testing | |
withVar('PING'); | |
withVar('PONG'); | |
withLet('PING'); | |
withLet('PONG'); | |
// suite | |
suite('var vs let', function () { | |
set('mintime', 2000); | |
set('concurrency', 500); | |
bench('with var', function () { | |
return withVar('PING'); | |
}); | |
bench('with let', function () { | |
return withLet('PING'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jakepusateri @EvanCarroll @tswaters I know a lot of you think that this kind of 'micro optimisation' is unnecessary and you're partly right, however; considering this de-opts the entire function that uses the 'let compound assignment' it therefore affects all the contained code within that function - which, in my opinion is where it no longer becomes 'micro'.
There is an issue with the matcha benchmarking lib, but apart from that the difference is still substantial. Consider the following real world example. (work in progress)
This is a redis parser I'm working on, namely the writable part of converting a cmd and it's args into the resp protocol.
Results
Benchmarked using 'benchmark' rather than match.