terser icon indicating copy to clipboard operation
terser copied to clipboard

Feature proposal: props reordering

Open dy opened this issue 5 years ago • 3 comments

Based on @subzey dict-tempering.

There's a method of minimizing JSONs compressed size by reordering props, provided in the mentioned tool.

There's a PR to color-name package, practically demonstrating 5-8% better compression.

The operation can be costly, but would be a nice feature to have that as an option for [extreme] optimization, wouldn't it?

dy avatar Mar 16 '21 13:03 dy

It's certainly not going to be trivial as such objects are not 100% identical. Consider:

obj = { a: 1, b: 2 }
// obj = { b: 2, a: 1 }
for (p in obj) {
    console.log(p);
}

If you uncomment the second line, the order will change.

WofWca avatar Jun 29 '21 21:06 WofWca

It's certainly not going to be trivial as such objects are not 100% identical.

That's why we could set the option in extreme optimization for advance usage. For example I known that my code base don't rely at all on object properties order (when I have order considerations I make use of Map)

Also.. could props reordering be also super useful for JavaScript engine optimizations?

JavaScript engines (V8, SpiderMonkey, etc.) uses hidden classes and one engine optimization we can do when writing JavaScript is to keep the property order constant to avoid polymorphism:

Always initialize object members in the same order.

See:

  • https://marcradziwill.com/blog/mastering-javascript-high-performance/#hiddenclasses
  • https://www.digitalocean.com/community/tutorials/js-v8-engine#tips-for-creating-objects
  • https://v8.dev/blog/fast-properties

I'm not a JavaScript engine expert.. so I'm not sure if this makes sense or not 🤔

yvele avatar Feb 19 '22 21:02 yvele

Surely enough, the object reordering should be turned off by default.

This feature would only make sense on a "static dictionary" like objects: large unordered singleton mappings of some static data. I.e., color names, emoji picture urls, webpack chunks, etc.

It would be nice to be able to mark them // #__unordered__ and let the compressor rearrange its parts if needed

subzey avatar Mar 17 '22 07:03 subzey