GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Performance: improve `objectWithoutPropertiesLoose` on V8 by romgrk · Pull Request #16357 · babel/babel

Conversation

@romgrk

@romgrk romgrk commented Mar 16, 2024

Copy link
Copy Markdown
Contributor

I've been doing some benchmarks and I've seen _objectWithoutPropertiesLoose pop up a few times. I looked at the implementation and noticed it could be improved a bit, allocating Object.keys() to iterate through it is a bit inefficient. I have changed the implementation to use for-in and .hasOwnProperty. Benchmarks: https://jsben.ch/lSR7E

I'm not familiar with your codebase, I have tentatively used "core-js-pure/features/object/has-own.js" but I'm not sure if that corresponds to Object.prototype.hasOwnProperty.

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@romgrk
perf: improve objectWithoutPropertiesLoose
f72d4d5
@babel-bot

babel-bot commented Mar 16, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@liuxingbaoyu

liuxingbaoyu commented Mar 16, 2024

Copy link
Copy Markdown
Member

Please change packages\babel-helpers\src\helpers.ts, then run make build, packages/babel-runtime-corejs3/helpers/esm/objectWithoutPropertiesLoose.js will be automatically generated.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@romgrk
lint
c3447c7
@romgrk

romgrk commented Mar 16, 2024

Copy link
Copy Markdown
Contributor Author

Thanks, I've updated the PR.

liuxingbaoyu reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@romgrk
lint
078753d
@JLHwung

JLHwung commented Mar 16, 2024

Copy link
Copy Markdown
Contributor

On macOS Safari the initial is faster than updated:

image

https://jsbench.me/1hltu3eit5/1

I migrated the benchmark to jsbench.me because the other one pops up click baits.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@armano2

armano2 commented Mar 17, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

sadly those benchmarks are not valid, as v8 is going to optimize some of this code

if we are looking at performance for in is slower than for

for approach is generally fastest in most scenarios as it can be "easier" optimized (that may vary from browser compiler version to version)

let o = null
o=_objectWithoutPropertiesLoose(o1, exclude)
console.log(o)

try running it without the extra loop


there seem to not be measurable time difference between both pieces of code

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@romgrk

romgrk commented Mar 17, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

On macOS Safari the initial is faster than updated:

Weid, JSC is usually happy with for-in and hasOwnProp. I'll try to do some digging. But I do think that optimizing for V8 is preferable, the difference both ways is in the 15-20% range, but chrome has a 65% traffic share at the moment.

sadly those benchmarks are not valid, as they v8 is going to optimize some of this code

Do you have a link to any relevant discussion?

try running it without the extra loop [...] there seem to not be measurable time difference between both pieces of code

What do you mean by without the loop? Do you have a better benchmark you could share to shed light on the measurements you see?

Wrt to the benchmark I have, I added a loop to ensure the function is optimized asap. This helper is what { x, ...rest } destructures to, which is a common pattern in many codebases so it's reasonable to assume the function is optimized by the engine in real world scenarios. I have also added multiple objects (o1 to o4) to ensure that the engine can't specialize for one particular object shape, which also wouldn't be the case outside of micro-benchmarks.

The other aspect that I didn't like is the Object.keys() allocation, which hides parts of its cost in the delayed garbage collector calls that are unlikely to be reflected in micro-benchmarks running in a tight CPU-bound loop. Unless escape analysis comes in play.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@romgrk

romgrk commented Mar 17, 2024

Copy link
Copy Markdown
Contributor Author

I've built a small benchmark to test on each engine, I can replicate the results you see with JSC. Despite the slowdown on JSC, I'd still argue that this implementation is better as the gains for V8 are more drastic, and more V8 users also means we should give more weight to that segment. Note that with this new implementation, V8 and JSC perform roughly the same, whereas the old one was considerably slower on V8 than on JSC.

But if you have more details on those upcoming V8 changes, please do share, I'd be happy to learn more.

### node: v21.7.1 ###
CASE: initial: #####################################............. 75.43% (2305 ops) (0.4338 ms/op)
CASE: updated: ################################################## 100% (3056 ops) (0.3272 ms/op)

### bun: 1.0.31 ###
CASE: initial: ################################################## 100% (3798 ops) (0.2633 ms/op)
CASE: updated: ############################################...... 88.92% (3377 ops) (0.2961 ms/op)

### gjs: 1.78.4 ###
CASE: initial: ################################################## 100% (1226 ops) (0.8157 ms/op)
CASE: updated: #################################################. 98.37% (1206 ops) (0.8292 ms/op)
liuxingbaoyu and wkronemeijer reacted with thumbs up emoji JLHwung reacted with heart emoji
All reactions
  • 👍 2 reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@liuxingbaoyu

liuxingbaoyu commented Mar 17, 2024

Copy link
Copy Markdown
Member

Thanks!
The performance changes look good to me, except for a bit of code size advice.

helpers.objectWithoutPropertiesLoose = helper("7.0.0-beta.0")`
  export default function _objectWithoutPropertiesLoose(source, excluded) {
    if (source == null) return {};
    var target = {};
    for (var key in source) {
      if ({}.hasOwnProperty.call(source, key)) {
        if (excluded.indexOf(key) < 0) target[key] = source[key];
      }
    }
    return target;
  }
`;
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@romgrk
lint
8cd9eae
@romgrk

romgrk commented Mar 17, 2024

Copy link
Copy Markdown
Contributor Author

I have integrated one change from the snippet above, moving var key inside the for loop. I'm not sure if you're also proposing to remove the newlines, but I've kept the same style as other helpers in the file, which don't seem harmful as they would be minified away. The change from Object.prototype.hasOwnProperty to {}.hasOwnPoperty is not equivalent, {} allocates a new object on the heap, I feel like it might be better to avoid that.

For more details on the Object.keys()/escape analysis/garbage collection: I've digged a bit, neither top-tier optimizer (turbofan for V8 and FTL for JSC) is able to optimize away the Object.keys() call in the original implementation. Which means it is indeed creating temporary arrays that need to be GC'ed, and that means the benchmark for the old implementation doesn't fully reflect the real cost, and thus the real-world performance loss on JSC is likely to be less than what is reflected in them.

armano2 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@romgrk
lint
a4f444d
@romgrk

romgrk commented Apr 3, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Some corrections, I've inspected JSC's output further and I think I was wrong when I said it didn't do escape analysis. I've added colors to the disassembly and I think it is able to optimize the Object.keys() away, which would explain the superior performance. This means that the benchmarks I showed above are likely to accurately reflect the real-world results.

Expand disassembly

image

I also previously wrote that chrome's market share is 65%, but including edge in the mix means V8 has a 70% usage currently, so I'd still recommend merging this one.

You have all the information, but if you don't feel like merging the PR feel free to close it.

liuxingbaoyu reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

liuxingbaoyu
liuxingbaoyu approved these changes Apr 3, 2024
View reviewed changes

@liuxingbaoyu liuxingbaoyu left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thank you for your detailed explanation!

In fact, we currently have terser enabled for most complex helpers, which compresses Object.prototype.hasOwnProperty into {}.hasOwnProperty. I'm surprised this affects performance.

https://jsbench.me/8jluk7693l/1

I did a simple test and it seems it doesn't affect performance in v8.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@romgrk

romgrk commented Apr 3, 2024

Copy link
Copy Markdown
Contributor Author

Yeah it's possible that V8 is able to perform ellision on that value through escape analysis. It's also possible that it's not, and the {} allocations need to be later cleared by the GC. AFAIK, allocating a value in a hot page is less expensive than the later GC costs. A micro-benchmark wouldn't show that cost though, because the CPU is running constantly it's less likely that the GC will kick-in. Can't say for sure without more research though, so by default I usually avoid allocations.

liuxingbaoyu reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JLHwung
JLHwung approved these changes Apr 3, 2024
View reviewed changes
@nicolo-ribaudo

nicolo-ribaudo commented Apr 4, 2024

Copy link
Copy Markdown
Member

I don't like market-share-based arguments (if we optimize for V8, then it gives even more advantage to V8 leading to it having a higher market share). However, given that the performance improvement here is higher than the perf regression in SM/JSC, I'm fine with this PR.

Nantris and SukkaW reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nicolo-ribaudo nicolo-ribaudo changed the title Performance: improve objectWithoutPropertiesLoose Performance: improve objectWithoutPropertiesLoose on V8 Apr 4, 2024
@nicolo-ribaudo
nicolo-ribaudo merged commit e96a05d into babel:main Apr 4, 2024
@nicolo-ribaudo nicolo-ribaudo added area: perf PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories labels Apr 4, 2024
@romgrk

romgrk commented Apr 4, 2024

Copy link
Copy Markdown
Contributor Author

I don't love having the ecosystem that much in the hands of google either, but JSC is already vastly more performant than V8 anyway, and is still more performant than V8 for this particular case even with the change.

nicolo-ribaudo reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@romgrk
romgrk deleted the perf-object-loose branch April 4, 2024 16:57
@davidbilling davidbilling mentioned this pull request May 20, 2024
[Snyk] Upgrade @babel/core from 7.24.4 to 7.24.5 davidbilling/ember-cli-time-input#104
Merged
@bee22193 bee22193 mentioned this pull request May 20, 2024
[Snyk] Upgrade: , bee22193/mnmlurl#1172
Open
This was referenced May 20, 2024
[Snyk] Upgrade: , nejidevelops/phase-0-intro-to-js-2-objects-lab#10
Open
[Snyk] Upgrade: , nejidevelops/phase-0-intro-to-js-2-objects-lab#11
Open
[Snyk] Upgrade: , nejidevelops/phase-1-destructuring-assignment#11
Open
@dev-javascript dev-javascript mentioned this pull request Jul 2, 2024
[Snyk] Upgrade @babel/standalone from 7.24.3 to 7.24.7 dev-javascript/string-to-react-component#7
Closed
This was referenced Jul 2, 2024
[Snyk] Upgrade: , tiff-es/fewpjs-adding-behavior-with-methods-dc-web-091619#190
Open
[Snyk] Upgrade: , tiff-es/js-data-structures-objects-lab-dc-web-091619#174
Open
[Snyk] Upgrade: , tiff-es/js-looping-and-iteration-looping-code-along-dc-web-091619#187
Open
[Snyk] Upgrade: , tiff-es/fewpjs-acting-on-events-dc-web-091619#179
Open
[Snyk] Upgrade: , tiff-es/fewpjs-document-onload-dc-web-091619#180
Open
[Snyk] Upgrade: , tiff-es/fewpjs-class-extensions-super-lab-online-web-sp-000#178
Open
[Snyk] Upgrade: , tiff-es/fewpjs-fns-as-first-class-data-array-o-functions-dc-web-091619#183
Open
[Snyk] Upgrade: , tiff-es/fewpjs-getter-and-setter-methods-dc-web-091619#189
Open
@bee22193 bee22193 mentioned this pull request Jul 3, 2024
[Snyk] Upgrade: , bee22193/mnmlurl#1206
Open
@anandgupta193 anandgupta193 mentioned this pull request Jul 3, 2024
[Snyk] Upgrade: , anandgupta193/sortNsuffle-react#1083
Open
@venkatarao-palaparthi venkatarao-palaparthi mentioned this pull request Jul 3, 2024
[Snyk] Upgrade: , , , , , , , , venkatarao-palaparthi/docs#439
Open
@richyhoopd richyhoopd mentioned this pull request Jul 3, 2024
[Snyk] Upgrade: , , richyhoopd/Video-App#1166
Open
@satoshinakamoto007 satoshinakamoto007 mentioned this pull request Jul 3, 2024
[Snyk] Upgrade: , , , satoshinakamoto007/docs#811
Open
This was referenced Jul 3, 2024
[Snyk] Upgrade: , tiff-es/fewpjs-adding-behavior-with-methods-dc-web-091619#191
Open
[Snyk] Upgrade: , tiff-es/js-data-structures-objects-lab-dc-web-091619#175
Open
[Snyk] Upgrade @babel/preset-env from 7.10.4 to 7.24.7 tiff-es/tiffany-portfolio-responsive#420
Open
[Snyk] Upgrade: , tiff-es/fewpjs-acting-on-events-dc-web-091619#180
Open
[Snyk] Upgrade: , tiff-es/fewpjs-getter-and-setter-methods-dc-web-091619#190
Open
[Snyk] Upgrade: , tiff-es/fewpjs-class-extensions-super-lab-online-web-sp-000#179
Open
[Snyk] Upgrade: , tiff-es/fewpjs-fns-as-first-class-data-array-o-functions-dc-web-091619#184
Open
[Snyk] Upgrade: , tiff-es/js-looping-and-iteration-looping-code-along-dc-web-091619#188
Open
@bee22193 bee22193 mentioned this pull request Jul 4, 2024
[Snyk] Upgrade: , bee22193/mnmlurl#1207
Open
@anandgupta193 anandgupta193 mentioned this pull request Jul 4, 2024
[Snyk] Upgrade: , anandgupta193/sortNsuffle-react#1084
Open
@mdfkbtc mdfkbtc mentioned this pull request Jul 4, 2024
[Snyk] Upgrade @babel/preset-env from 7.5.0 to 7.24.7 mdfkbtc/veles-mobile-wallet-mdfkbtc#1761
Open
This was referenced Jul 4, 2024
[Snyk] Upgrade: , , richyhoopd/Video-App#1167
Open
[Snyk] Upgrade: , , richyhoopd/RichyFlix#878
Open
@tiff-es tiff-es mentioned this pull request Jul 5, 2024
[Snyk] Upgrade: , tiff-es/fewpjs-document-onload-dc-web-091619#181
Open
@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 Jul 5, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@JLHwung JLHwung JLHwung approved these changes
@liuxingbaoyu liuxingbaoyu liuxingbaoyu approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

area: perf outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@romgrk @babel-bot @liuxingbaoyu @JLHwung @armano2 @nicolo-ribaudo
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.