4 ms·
> I automatically assume JavaScript developers to be completely oblivious to this entire class of software development knowledge. Surprisingly, In JavaScript
by gildas 7y ago
> I automatically assume JavaScript developers to be completely oblivious to this entire class of software development knowledge.
Surprisingly, In JavaScript the technique described in the article is quite efficient on Firefox whereas the gain is almost negligible on Chrome.
https://jsperf.com/mispredicted-branches https://jsperf.com/mispredicted-branches
Edit: Jsperf seeems to be down. Here are the 2 snippets of code I tested:
// Unoptimized
let howmany = 5000000;
let index = 0;
const out = [];
while (howmany) {
const val = Math.floor(Number.MAX_SAFE_INTEGER * Math.random());
if (val % 2) {
out[index] = val;
index += 1;
}
howmany--;
}
// Optimized for branch prediction
let howmany = 5000000;
let index = 0;
const out = [];
while (howmany) {
const val = Math.floor(Number.MAX_SAFE_INTEGER * Math.random());
out[index] = val;
index += (val & 1);
howmany--;
}
- naikrovek 7y agoYeah, of course performance gains can be had in JavaScript, and I will add to that fact with the experience that I've had when working with JavaScript developers: only one that I've ever known has ever considered performance and worked to produce performant JS before multiple users complained and an issue was raised. All others simply do not care about Javascript performance until there is an issue created to address it. This single person is also the only JS developer I know who has any clue how to actually improve performance in existing code.
- littlestymaar 7y agoThis matches my experience as well (as a former full-time JavaScript dev), but in most cases, where JavaScript devs don't care about performance, you don't even need to understand how the machine works to speed things up. 90% of the time, the algorithmic complexity isn't thought at all and there are plenty of quadratic (or worse) behaviors everywhere.
- djokkataja 7y agoI tried both of those out in the console in firefox and chrome and found that in chrome the optimized version was about 10% faster, but in firefox the unoptimized version was about 5% faster. Also it ran 30x faster in chrome... (~200ms vs ~6000ms)