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

Make loop stop when it doesn't have any more obj to check #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luanmuniz
Copy link

@luanmuniz luanmuniz commented May 23, 2019

With this change, if you have and object like { foo: "bar" } and you do dlv(obj, 'foo.bar.foobar.0.whatever') it will run twice instead of 5 times.

I have objects that are 20 degrees deep in a very performance sensitive app and this saves me quite a few loops

The bundlesize is a little bigger now.

Wrote 130 B: dlv.js.gz
         99 B: dlv.js.br
Wrote 130 B: dlv.es.js.gz
        100 B: dlv.es.js.br
Wrote 208 B: dlv.umd.js.gz
        175 B: dlv.umd.js.br

Maybe #32 (comment) can be considered as well, I'm just not sure on the performance downsides of for vs .some

With this change, if you have and object like { foo: "bar" } and you do dlv(obj, 'foo.bar.foobar.0.whatever')
it will run twice instead of 5 times.

Signed-off-by: Luan Muniz <[email protected]>
@RReverser
Copy link
Contributor

The bundlesize is a little bigger now, but IMO the performance gain is worth it.

Why? No-op spinning loop shouldn't have any significant impact (and might be even optimised away altogether).

@luanmuniz
Copy link
Author

The bundlesize is a little bigger now, but IMO the performance gain is worth it.

Why? No-op spinning loop shouldn't have any significant impact (and might be even optimized away altogether).

The objects I'm handling are quite big and in some of my functions, this change saves me hundreds of loop, object manipulation, and conditional checks.

For small objects indeed there is no gain. But for bigger ones that's not the case

@RReverser
Copy link
Contributor

this change saves me hundreds of loop, object manipulation, and conditional checks

Did you benchmark it? That's the only way to prove that change is actually worth it, it's not like JavaScript is interpreted literally. (also FWIW there is no object manipulation involved after the first condition hit)

@luanmuniz
Copy link
Author

https://esbench.com/bench/5ce7f6624cd7e6009ef62543

Screen Shot 2019-05-24 at 11 05 17

This is a very small change. I have objects double that size running in loops of hundreds. In my case you can increase this performance gain by 100x if you zoom out to my application

@RReverser
Copy link
Contributor

In my case you can increase this performance gain by 100x if you zoom out to my application

How so? Proportions shouldn't change from either number of times you run it or size of the object (only from level of nesting maybe).

Moreover, if your application is doing anything else besides property access, they're likely to become completely negligible compared to other operations.

Finally, if you do care about performance more than the size (which is the main target of this helper AFAIK), then, as mentioned before, you probably shouldn't use dynamic property access in the first place and instead use optional chaining operator instead (e.g. via Babel plugin - https://www.npmjs.com/package/babel-plugin-transform-optional-chaining):

screenshot-esbench com-2019 05 24-15-19-11

@luanmuniz
Copy link
Author

In my case you can increase this performance gain by 100x if you zoom out to my application

How so? Proportions shouldn't change from either number of times you run it or size of the object (only from level of nesting maybe).

I call the function several times, not only once. So it's 100x because I call it 100x. That's why the if you zoom out to my application part

Moreover, if your application is doing anything else besides property access, they're likely to become completely negligible compared to other operations.

What do you mean?

Finally, if you do care about performance more than the size (which is the main target of this helper AFAIK), then, as mentioned before, you probably shouldn't use dynamic property access in the first place and instead use optional chaining operator instead (e.g. via Babel plugin - https://www.npmjs.com/package/babel-plugin-transform-optional-chaining):

I was searching for something I could use both frontend and backend and I don't use babel in my nodejs apps. But maybe is something that I need to look at since the performance difference is that much.

I assumed that since the transpiler adds code for other parts, the performance improvement that I gain over one thing would be lost by other features.

I will actually look into the babel plugin. Thanks for the suggestion.
Can you send me the benchmark link so I can check the setup? I'm curious about that.

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