Skip to content

[JS] Prefer Object.hasOwn over hasOwnProperty. NFC - #28129

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:hasOwn
Nov 2, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:hasOwn

Conversation

@sbc100

@sbc100 sbc100 commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

In compile-time JS code (src/, tools/, and compile-time helpers in
libwebgl.js), use Object.hasOwn directly instead of
hasOwnProperty.

In runtime JS library code, introduce an internal $hasOwn helper in
libcore.js that aliases Object.hasOwn when targeting modern browser
versions (MIN_FIREFOX_VERSION >= 92, MIN_CHROME_VERSION >= 93, and
MIN_SAFARI_VERSION >= 150400) and falls back to
Object.prototype.hasOwnProperty.call otherwise.

Note that this change comes with minor codesize change, in some cases an
improvement and in some a regression. Note that if we were to bump
our default min browser versions we would always see an improvement
(perhaps we should always target modern browser in our codesize tests?)

See #28125

@sbc100
sbc100 requested review from dschuff and kripken November 1, 2026 20:56

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request replaces hasOwnProperty calls with Object.hasOwn or a new $hasOwn helper across the codebase, enforcing this pattern via ESLint's prefer-object-has-own rule. The review feedback points out a compatibility issue in src/lib/libcore.js where MIN_NODE_VERSION < 160900 should be added to the fallback condition to ensure older Node.js environments are properly supported.

Comment thread src/lib/libcore.js Resolved
In compile-time JS code (`src/`, `tools/`, and compile-time helpers in
`libwebgl.js`), use `Object.hasOwn` directly instead of
`hasOwnProperty`.

In runtime JS library code, introduce an internal `$hasOwn` helper in
`libcore.js` that aliases `Object.hasOwn` when targeting modern browser
versions (`MIN_FIREFOX_VERSION >= 92`, `MIN_CHROME_VERSION >= 93`, and
`MIN_SAFARI_VERSION >= 150400`) and falls back to
`Object.prototype.hasOwnProperty.call` otherwise.

Note that this change comes with minor codesize change, in some cases an
improvement and in some a regression.   Note that if we were to bump
our default min browser versions we would always see an improvement
(perhaps we should always target modern browser in our codesize tests?)

See emscripten-core#28125
@sbc100
sbc100 merged commit 0ed6ce2 into emscripten-core:main Nov 2, 2026
19 of 24 checks passed
@sbc100
sbc100 deleted the hasOwn branch November 2, 2026 19:19
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 2, 2026
Follow-up to emscripten-core#28129:

- Enable `prefer-exponentiation-operator`, `prefer-object-spread`,
  `prefer-rest-params`, `prefer-spread`, and
  `logical-assignment-operators` in `eslint.config.mjs`.
- Use `Object.values`, direct integer indexing, or `??=` in places
  where `$hasOwn` is not needed.
- Use `Map` instead of plain `{}` objects for lookup tables in
  `acorn-optimizer.mjs` and `libglfw.js`.
- Replace legacy `Array.prototype.slice.call`, `.apply`, `.indexOf`,
  `Math.pow`, and `arr[arr.length - 1]` idioms with `.slice`,
  `Array.from`, spread syntax, `.includes`, `**` / `Math.hypot`, and
  `.at(-1)`.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 2, 2026
Follow-up to emscripten-core#28129:

- Enable `prefer-exponentiation-operator`, `prefer-object-spread`,
  `prefer-rest-params`, `prefer-spread`, and
  `logical-assignment-operators` in `eslint.config.mjs`.
- Use `Object.values`, direct integer indexing, or `??=` in places
  where `$hasOwn` is not needed.
- Use `Map` instead of plain `{}` objects for lookup tables in
  `acorn-optimizer.mjs` and `libglfw.js`.
- Replace legacy `Array.prototype.slice.call`, `.apply`, `.indexOf`,
  `Math.pow`, and `arr[arr.length - 1]` idioms with `.slice`,
  `Array.from`, spread syntax, `.includes`, `**` / `Math.hypot`, and
  `.at(-1)`.
basvandijk added a commit to basvandijk/emscripten that referenced this pull request Nov 2, 2026
emscripten-core#28129 added `$hasOwn` to libcore.js. It aliases `Object.hasOwn` when the
minimum browser versions support it and falls back to
`Object.prototype.hasOwnProperty.call` otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 2, 2026
Bump the default target browser versions in `src/settings.js`:
- `MIN_CHROME_VERSION`: 85 -> 93
- `MIN_FIREFOX_VERSION`: 79 -> 92
- `MIN_SAFARI_VERSION`: 150000 -> 150400

This allows `$hasOwn` in `libcore.js` (introduced in emscripten-core#28129) to use the
`Object.hasOwn` fast path by default.

See: emscripten-core#28125
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 2, 2026
Bump the default target browser versions in `src/settings.js`:
- `MIN_CHROME_VERSION`: 85 -> 93
- `MIN_FIREFOX_VERSION`: 79 -> 92
- `MIN_SAFARI_VERSION`: 150000 -> 150400

This allows `$hasOwn` in `libcore.js` (introduced in emscripten-core#28129) to use the
`Object.hasOwn` fast path by default.

See: emscripten-core#28125
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 3, 2026
Follow-up to emscripten-core#28129:

- Enable `prefer-exponentiation-operator`, `prefer-object-spread`,
  `prefer-rest-params`, `prefer-spread`, and
  `logical-assignment-operators` in `eslint.config.mjs`.
- Use `Object.values`, direct integer indexing, or `??=` in places
  where `$hasOwn` is not needed.
- Use `Map` instead of plain `{}` objects for lookup tables in
  `acorn-optimizer.mjs` and `libglfw.js`.
- Replace legacy `.apply`, `.indexOf`, `Math.pow`, and
  `arr[arr.length - 1]` idioms with `.call` / spread syntax,
  `.includes`, `**` / `Math.hypot`, and `.at(-1)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants