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

perf: avoid array iterators over objects #12264

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

GalacticHypernova
Copy link
Contributor

@GalacticHypernova GalacticHypernova commented Oct 25, 2024

Object iterators like Object.keys, Object.values, and Object.entries, when used for iteration, create a temporary intermediate array by internally making another iteration, essentially doubling the memory usage and execution time. Using object iterators will avoid that intermediate array and the extra internal iteration.

Benchmark (each section ran separately to prevent any inline cache from altering results):

const largeObject = {}
for (let i=0; i<100000;i++) {
    largeObject[i] = i
}

console.time("array method")
for (const num of Object.keys(largeObject)) {
    // It doesn't matter what happens within the loop as the main slowdown is the keys iterator
}
console.timeEnd("array method")

console.time("obj method")
for (const num in largeObject) {
    // It doesn't matter what happens within the loop as the main slowdown is the keys iterator
}
console.timeEnd("obj method")

Performance results:
image
image

Memory results:
image
image

In places where prototype pollution may be a concern, a review would be appreciated so I could add own property checks

Copy link

pkg-pr-new bot commented Oct 25, 2024

Open in Stackblitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@12264

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@12264

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@12264

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@12264

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@12264

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@12264

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@12264

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@12264

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@12264

vue

pnpm add https://pkg.pr.new/vue@12264

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@12264

commit: 3123a16

Copy link

github-actions bot commented Oct 25, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB (-13 B) 38 kB (-2 B) 34.2 kB (+42 B)
vue.global.prod.js 159 kB (-13 B) 57.9 kB (-2 B) 51.4 kB (-13 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.9 kB 18.3 kB 16.7 kB
createApp 55 kB 21.3 kB 19.4 kB
createSSRApp 59 kB 23 kB 20.9 kB
defineCustomElement 59.7 kB (-13 B) 22.8 kB (-3 B) 20.8 kB (-70 B)
overall 68.7 kB 26.3 kB 24 kB

@GalacticHypernova GalacticHypernova marked this pull request as ready for review October 26, 2024 07:26
@edison1105
Copy link
Member

a side note:
ctx.modelDecls and ctx.userImports are usually not large objects.

@edison1105 edison1105 added 🧹 p1-chore Priority 1: this doesn't change code behavior. ready for review This PR requires more reviews labels Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧹 p1-chore Priority 1: this doesn't change code behavior. ready for review This PR requires more reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants