Skip to content

Commit 37d70bd

Browse files
committed
Avoid calling hash.update multiple times in ConcatenatedModules
@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.
1 parent 8fdf411 commit 37d70bd

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/optimize/ConcatenatedModule.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,15 @@ class ConcatenatedModule extends Module {
295295
}
296296

297297
_createIdentifier() {
298-
const hash = crypto.createHash("md5");
299-
this._orderedConcatenationList.forEach(info => {
300-
if(info.type === "concatenated") {
301-
hash.update(info.module.identifier() + " ");
298+
let orderedConcatenationListIdentifiers = "";
299+
for(let i = 0; i < this._orderedConcatenationList.length; i++) {
300+
if(this._orderedConcatenationList[i].type === "concatenated") {
301+
orderedConcatenationListIdentifiers += this._orderedConcatenationList[i].module.identifier();
302+
orderedConcatenationListIdentifiers += " ";
302303
}
303-
});
304+
}
305+
const hash = crypto.createHash("md5");
306+
hash.update(orderedConcatenationListIdentifiers);
304307
return this.rootModule.identifier() + " " + hash.digest("hex");
305308
}
306309

0 commit comments

Comments
 (0)