Skip to content

Commit

Permalink
Avoid calling hash.update multiple times in ConcatenatedModules
Browse files Browse the repository at this point in the history
@sokra did some profiling and it seems that calling crypto.update
multiple times is slower than calling it once with a large string.

> As explanation I would guess update calls into node.js C++ + OpenSSL
> while concatenating strings is very optimized in JIT.

To take better advantage of this information, I have rewritten this
performance-sensitive code to build up a concatenated string and then
hash it at the end.

Since we are thinking about performance here, I opted for a regular for
loop instead of using the forEach iterator. I also did some basic
benchmarking of string concatenation strategies and discovered that
separating out the addition of the extra space into its own
concatenation line instead of tacking it on to the end of a single
concatenation might just barely have the edge in terms of performance. I
think it is also very readable this way, so it seems like a good
tradeoff to make: a little lost conciseness for speed.
  • Loading branch information
lencioni committed Nov 23, 2017
1 parent 8fdf411 commit 37d70bd
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/optimize/ConcatenatedModule.js
Expand Up @@ -295,12 +295,15 @@ class ConcatenatedModule extends Module {
}

_createIdentifier() {
const hash = crypto.createHash("md5");
this._orderedConcatenationList.forEach(info => {
if(info.type === "concatenated") {
hash.update(info.module.identifier() + " ");
let orderedConcatenationListIdentifiers = "";
for(let i = 0; i < this._orderedConcatenationList.length; i++) {
if(this._orderedConcatenationList[i].type === "concatenated") {
orderedConcatenationListIdentifiers += this._orderedConcatenationList[i].module.identifier();
orderedConcatenationListIdentifiers += " ";
}
});
}
const hash = crypto.createHash("md5");
hash.update(orderedConcatenationListIdentifiers);
return this.rootModule.identifier() + " " + hash.digest("hex");
}

Expand Down

0 comments on commit 37d70bd

Please sign in to comment.