Skip to content

Use for...of loops to avoid object inheritance#3381

Open
yawaramin wants to merge 3 commits into
bigskysoftware:devfrom
yawaramin:for-of-dev
Open

Use for...of loops to avoid object inheritance#3381
yawaramin wants to merge 3 commits into
bigskysoftware:devfrom
yawaramin:for-of-dev

Conversation

@yawaramin

Copy link
Copy Markdown
Contributor

Description

Stop using for...in loops because they iterate over the objects' entire inheritance tree. Instead, only iterate over the objects' own keys.

This should speed htmx up a bit, but I haven't benchmarked it! It's more of a correctness issue.

Also use Object.assign() instead of reimplementing it.

Testing

Ran npm run test - all tests passing. This is purely an internal (refactoring) change so no new tests needed.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

@alexpetros alexpetros left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Generally approve, just left a small comment

Comment thread src/htmx.js
}
// @ts-ignore tsc doesn't seem to properly handle types merging
return obj1
return Object.assign({}, obj1, obj2)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

caniuse says this goes back to 2015, basically: https://caniuse.com/mdn-javascript_builtins_object_assign

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, OK, do we have a policy on minimum supported browser version?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not an official one, just noting it to say that I think it's okay!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

yeah this is fully supported as this is ES6/ES2015 and we already have many features used from this version when we did v2.0. We should probably decide on a better baseline we want to rule in or out like ES2020 or ES2018 as i've seen ES2016 things like array.includes is already in the code base

Comment thread src/htmx.js Outdated
detail = { value: detail }
}
triggerEvent(elt, eventName, detail)
for (const eventName of Object.keys(triggers || {})) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think const triggers = parseJSON(triggerBody) || {} would be better

@alexpetros alexpetros added the ready for review Issues that are ready to be considered for merging label Jul 22, 2025
@alexpetros

Copy link
Copy Markdown
Collaborator

I think this is good 👍

Stop using `for...in` loops because they iterate over the objects'
entire inheritance tree. Instead, only iterate over the objects' _own_
keys.

This _should_ speed htmx up a bit, but I haven't benchmarked it! It's
more of a correctness issue.

Also use `Object.assign()` instead of reimplementing it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for review Issues that are ready to be considered for merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants