eta icon indicating copy to clipboard operation
eta copied to clipboard

Peculiar performance with useWith

Open bribes opened this issue 1 year ago • 5 comments

Is your feature request related to a problem? Please describe. useWith is slower then just setting the it object to globalThis in the actual file.

Describe the solution you'd like I am confused as to why, I used Object.entries(it).forEach(([key, value]) => globalThis[key] = value) in the template file and it was actually faster then using the useWith setting (in my testing)

Additional context Will add tommorow, did some performance testing.

bribes avatar Aug 26 '24 03:08 bribes

That's fascinating! To be honest, I'm not surprised -- useWith has been recommended against for a while, so it's likely not optimized by most common JS runtimes.

bgub avatar Aug 26 '24 18:08 bgub

Yeah seems like with() is always the slowest at first but gets faster overtime when it is run multiple times but is still slower then globalThis. (This is not accurate):

with() 0.007s
globalThis 0.0004s

You can just set it up using functionHeader: 'Object.entries(Object.assign({},it)||{}).forEach(([k,v])=>globalThis[k]=v)' (Not using {...} spread as I believe it is slightly slower. I could probably implement this directly but not sure if you want that. You should probably test it yourself.

bribes avatar Aug 26 '24 23:08 bribes

@bribes fascinating! I probably don't have time to test it right now, but it would be great if you wanted to add it to the documentation!

bgub avatar Aug 29 '24 09:08 bgub

Yeah seems like with() is always the slowest at first but gets faster overtime when it is run multiple times but is still slower then globalThis. (This is not accurate):

with() 0.007s
globalThis 0.0004s

You can just set it up using functionHeader: 'Object.entries(Object.assign({},it)||{}).forEach(([k,v])=>globalThis[k]=v)' (Not using {...} spread as I believe it is slightly slower. I could probably implement this directly but not sure if you want that. You should probably test it yourself.

Don't do your "recommended solution" (and it should not be mentionned in the documentation) as it will lead to race conditions.

GreenFlag31 avatar May 09 '25 19:05 GreenFlag31

Yeah seems like with() is always the slowest at first but gets faster overtime when it is run multiple times but is still slower then globalThis. (This is not accurate):

with() 0.007s
globalThis 0.0004s

You can just set it up using functionHeader: 'Object.entries(Object.assign({},it)||{}).forEach(([k,v])=>globalThis[k]=v)' (Not using {...} spread as I believe it is slightly slower. I could probably implement this directly but not sure if you want that. You should probably test it yourself.

Don't do your "recommended solution" (and it should not be mentionned in the documentation) as it will lead to race conditions.

Oh damn I didn't realize this, I probably won't have time to make a pull request to remove from documentation anytime soon, could you do it? I can do it whenever I have time though.

bribes avatar May 10 '25 06:05 bribes

I just updated the docs to remove that suggestion. @bribes thanks for the heads up.

bgub avatar Nov 12 '25 21:11 bgub