webpack / webpack-sources

Source code handling classes for webpack
MIT License
261 stars 71 forks source link

remove unnecessary memory allocation from ConcatSource and ReplaceSource #30

Closed MagicDuck closed 6 years ago

MagicDuck commented 6 years ago

The code in ConcatSource::source() and ReplaceSource::_replaceString() is using array.join("") which causes allocation of new strings. Using simple concatenation with += instead causes node to create what it calls something like "concatenated strings" internally which reuse the memory of the original strings being concatenated (this is probably possible since strings are immutable).

This problem was discovered by recording a "memory allocation profile" using chrome dev tools for the duration of the whole webpack build.

Before: image

After change: image

MagicDuck commented 6 years ago

I will create a PR soon.

filipesilva commented 6 years ago

Think this can be closed since https://github.com/webpack/webpack-sources/pull/31 was merged.

joshwiens commented 6 years ago

Thanks @MagicDuck

MagicDuck commented 6 years ago

No worries :) There's a bunch more, on a more minor level, that i have seen in the source-list-map project. I want to see what can be done. The most bizarre one is this: https://github.com/webpack/source-list-map/blob/master/lib/helpers.js I am not sure why dev tools is reporting getNumberOfLines as allocating tens of mb in my case...