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

Object3D: getObjectsByProperty improvement #27223

Closed
agargaro opened this issue Nov 20, 2023 · 1 comment · Fixed by #27225
Closed

Object3D: getObjectsByProperty improvement #27223

agargaro opened this issue Nov 20, 2023 · 1 comment · Fixed by #27225

Comments

@agargaro
Copy link
Contributor

Description

The getObjectsByProperty method can be greatly improved by sharing the same array, instead of creating new ones and concatenating them.

I have created an example where performance can be checked.

If you want I can do PR.

Reproduction steps

I created a scene with 5000 children and called the getObjectsByProperty method to select them all.

Code

Current function:

getObjectsByProperty( name, value ) {

		let result = [];

		if ( this[ name ] === value ) result.push( this );

		for ( let i = 0, l = this.children.length; i < l; i ++ ) {

			const childResult = this.children[ i ].getObjectsByProperty( name, value );

			if ( childResult.length > 0 ) {

				result = result.concat( childResult );

			}

		}

		return result;

	}

Function with shared array reference:

function getObjectsByProperty2(name, value, result = []) {
  if (this[name] === value) result.push(this);

  for (let i = 0, l = this.children.length; i < l; i++) {
    this.children[i].getObjectsByProperty2(name, value, result);
  }

  return result;
};

Live example

https://jsfiddle.net/agargaro/qyh6zxkg/4/

Screenshots

No response

Version

r158

Device

No response

Browser

No response

OS

No response

@Mugen87
Copy link
Collaborator

Mugen87 commented Nov 21, 2023

I agree this is a good suggestion. We have similar result or target parameters in other methods as well.

If you want I can do PR.

That would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants