Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize computed properties output (byte-wise) #6652

Merged
merged 3 commits into from Oct 20, 2023

Conversation

Andarist
Copy link
Member

@Andarist Andarist commented Oct 31, 2017

Q                       A
Fixed Issues? n/a
Patch: Bug Fix? no
Major: Breaking Change? no
Minor: New Feature? no
Tests Added + Pass? Yes
Documentation PR no
Any Dependency Changes? no

It is a small "optimization" of the computed properties output. The change basically wraps (if possible) previous element - initProps object or previous defineProperty call when "pushing" new defineProperty calls.

It leverages the fact that defineProperty returns the passed object, therefore in most cases we do not need to create temporary references and stuff.

babel-plugin-transform-computed-properties/test/fixtures/spec/multiple illustrates this nicely.

Before

var _obj;
var obj = (_obj = {}, babelHelpers.defineProperty(_obj, "x" + foo, "heh"), babelHelpers.defineProperty(_obj, "y" + bar, "noo"), _obj);

After

var obj = babelHelpers.defineProperty(babelHelpers.defineProperty({}, "x" + foo, "heh"), "y" + bar, "noo");

Not only it saves some bytes in the output, it is probably more friendly for the minifiers too.

@babel-bot
Copy link
Collaborator

babel-bot commented Oct 31, 2017

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/55254/

@Andarist Andarist added the PR: Polish 💅 A type of pull request used for our changelog categories label Oct 31, 2017
var heh = "heh";
var noo = "noo";
var foo = "foo";

var obj = {
Copy link
Member

@xtuc xtuc Dec 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how it worked before, either foo and bar were undefined in the score or you now re-define them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was purely a fixture/snapshot test, I've added also exec.js and that's why I had to add those to the scope in actual/expected fixtures (like you have noticed - they were previously undefined).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that make sense

@xtuc
Copy link
Member

xtuc commented Nov 6, 2018

@Andarist do we stil want this change?

@Andarist
Copy link
Member Author

Andarist commented Nov 6, 2018

I would be in favour of merging this (after rebase ofc), but unfortunately noone (except you) has reviewed this.

@xtuc
Copy link
Member

xtuc commented Nov 6, 2018

My review wasn't very useful, to push this forward we could discuss about it during our next meeting. Could you please add it to our agenda here babel/notes#73. thanks

Copy link
Member

@loganfsmyth loganfsmyth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what the actual byte difference ends up being once GZip is taken into account?

Is it worth exploring a new helper to optimize this instead? I don't know if that would be harder to keep the property evaluation ordering.

var heh = "heh";
var noo = "noo";
var foo = "foo";
var obj = babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty({}, "x" + heh, "heh"), "y" + noo, "noo"), foo, "foo1"), "foo", "foo2"), "bar", "bar");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with this, we should probably have a cap on the nesting here, or else we risk objects with lots of keys creating extremely deep AST structures.

@nicolo-ribaudo nicolo-ribaudo changed the base branch from master to main August 28, 2023 17:59
@nicolo-ribaudo
Copy link
Member

@JLHwung @liuxingbaoyu This is something that we can still do. I rebased this PR, and added a cap as suggested in #6652 (comment).

@nicolo-ribaudo nicolo-ribaudo added PR: Output optimization 🔬 A type of pull request used for our changelog categories and removed PR: Polish 💅 A type of pull request used for our changelog categories labels Aug 28, 2023
@Andarist
Copy link
Member Author

Landing this PR was not my 2023 bingo card… 😝

@nicolo-ribaudo
Copy link
Member

Life is full of surprises 😛

@nicolo-ribaudo nicolo-ribaudo merged commit 2c5fb77 into babel:main Oct 20, 2023
48 checks passed
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Feb 28, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Output optimization 🔬 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants