Skip to content

Math: Make use of Static initialization blocks.#33140

Merged
Mugen87 merged 1 commit into
mrdoob:devfrom
PoseidonEnergy:move-prototype-assignments-1
Mar 12, 2026
Merged

Math: Make use of Static initialization blocks.#33140
Mugen87 merged 1 commit into
mrdoob:devfrom
PoseidonEnergy:move-prototype-assignments-1

Conversation

@PoseidonEnergy
Copy link
Copy Markdown
Contributor

@PoseidonEnergy PoseidonEnergy commented Mar 8, 2026

Description

Moves all of the MatrixN.isMatrixN = true and VectorN.isVectorN = true assignments out of the constructors so they aren't unnecessarily re-set for every new instance.

Side note: My IDE (WebStorm) is displaying an error for the static { ... } block because ecmaVersion in eslint.config.js is set to 2018.

ECMA 2022 Static Initialization Blocks probably should be supported in three.js: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks

@Mugen87 Related: #33128

image

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 8, 2026

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 359.27
85.31
359.27
85.31
+0 B
-5 B
WebGPU 630.16
174.95
630.16
174.95
+0 B
+0 B
WebGPU Nodes 628.75
174.7
628.75
174.7
+0 B
+0 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 491.13
119.74
491.47
119.97
+342 B
+224 B
WebGPU 703.41
189.9
703.38
189.9
-36 B
+1 B
WebGPU Nodes 652.64
177.33
652.61
177.29
-36 B
-36 B

@mrdoob
Copy link
Copy Markdown
Owner

mrdoob commented Mar 9, 2026

Oh, this is nice.

@mrdoob
Copy link
Copy Markdown
Owner

mrdoob commented Mar 9, 2026

Seems like Webpack supports the class field + private fields since 5.36.0 (2021).
Maybe we can give ECMA 2022 a go.

@Mugen87
Copy link
Copy Markdown
Collaborator

Mugen87 commented Mar 9, 2026

I still hesitate, tbh^^. There is no ECMA 2022 feature that looks like a must have and I'm not sure it's worth to risk breakage for this type of refactoring.

@gkjohnson
Copy link
Copy Markdown
Collaborator

gkjohnson commented Mar 9, 2026

There is no ECMA 2022 feature that looks like a must have

Support for import.meta.url syntax to improve KTX2 and DRACOLoader ergonomics is something I raise every once and awhile and dropping support for Webpack 4.x would enable.

@mrdoob
Copy link
Copy Markdown
Owner

mrdoob commented Mar 9, 2026

I still hesitate, tbh^^. There is no ECMA 2022 feature that looks like a must have and I'm not sure it's worth to risk breakage for this type of refactoring.

Do you remember how old was the Webpack that caused breakage last time?

@Mugen87
Copy link
Copy Markdown
Collaborator

Mugen87 commented Mar 9, 2026

It was definitely Webpack 4.x.

@PoseidonEnergy PoseidonEnergy changed the title Don't set class prototype fields (e.g. 'prototype.isMatrix2 = true`) in constructors Don't set class prototype fields (e.g. prototype.isMatrix2 = true) in constructors Mar 9, 2026
@gkjohnson
Copy link
Copy Markdown
Collaborator

5.x was first released back in 2020 and the last 4.x release was in September 2023, almost three years ago (v4.47.0). The last release before that (v4.46.0) was in November 2021.

At this point (I'd argue far before) anyone who has refused to upgrade to Webpack 5 should not be surprised that it's breaking when updating some dependencies. Realistically that download number for 4.x will probably never be down as low as we'd like it to be to drop support but at some point we need to be able to make a decision that will enable us to be able use more modern Javascript features to improve both the usability of the project and our own development experience.

@mrdoob
Copy link
Copy Markdown
Owner

mrdoob commented Mar 11, 2026

Agreed. We need to keep moving.

@Mugen87
Copy link
Copy Markdown
Collaborator

Mugen87 commented Mar 11, 2026

Okay, let's do it. Hopefully, it won't get too bumpy for users.

To resolve the CI complains, we need to merge #33128 first which increases the ECMA version in the ESLint config. I'll reopen and merge the PR.

@Mugen87
Copy link
Copy Markdown
Collaborator

Mugen87 commented Mar 11, 2026

@PoseidonEnergy Do you mind rebasing the PR so the new ESLint settings from #33128 are honored?

@Mugen87 Mugen87 added this to the r184 milestone Mar 11, 2026
@PoseidonEnergy PoseidonEnergy force-pushed the move-prototype-assignments-1 branch from 1a69123 to 04dad3c Compare March 12, 2026 00:38
@PoseidonEnergy
Copy link
Copy Markdown
Contributor Author

@Mugen87 Thanks, done

@Mugen87 Mugen87 changed the title Don't set class prototype fields (e.g. prototype.isMatrix2 = true) in constructors Math: Don't set class prototype fields in constructors. Mar 12, 2026
@Mugen87 Mugen87 changed the title Math: Don't set class prototype fields in constructors. Math: Make use of Static initialization blocks. Mar 12, 2026
@Mugen87 Mugen87 merged commit 4fad923 into mrdoob:dev Mar 12, 2026
10 checks passed
bzztbomb added a commit to bzztbomb/DefinitelyTyped that referenced this pull request Apr 28, 2026
Most ThreeJS classes have a is* property on the prototype.  This allows
for typechecks without using instanceof.  Recently the vector and
matrix classes change to initialize this property using static initialization block.
(PR here: mrdoob/three.js#33140)

The types were updated to be 'static readonly isVector3: boolean' which means
one can only access isVector3 like this: Vector3.isVector3 which is not useful.

This commit reverts the type update and restores the previous functionality.
bzztbomb added a commit to bzztbomb/three-ts-types that referenced this pull request Apr 28, 2026
Most ThreeJS classes have a is* property on the prototype.  This allows
for typechecks without using instanceof.  Recently the vector and
matrix classes change to initialize this property using static initialization block.
(PR here: mrdoob/three.js#33140)

The types were updated to be 'static readonly isVector3: boolean' which means
one can only access isVector3 like this: Vector3.isVector3 which is not useful.
While the initialization of the property changed, the shape of the class did not.

This commit reverts the type update and restores the previous functionality.
Methuselah96 pushed a commit to three-types/three-ts-types that referenced this pull request Apr 29, 2026
Most ThreeJS classes have a is* property on the prototype.  This allows
for typechecks without using instanceof.  Recently the vector and
matrix classes change to initialize this property using static initialization block.
(PR here: mrdoob/three.js#33140)

The types were updated to be 'static readonly isVector3: boolean' which means
one can only access isVector3 like this: Vector3.isVector3 which is not useful.
While the initialization of the property changed, the shape of the class did not.

This commit reverts the type update and restores the previous functionality.
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.

4 participants