throttler icon indicating copy to clipboard operation
throttler copied to clipboard

Performance Improvement: use `Map` for storage in `ThrottlerStorageService`

Open melonges opened this issue 1 year ago • 3 comments

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

The current implementation of the ThrottlerStorageService uses a plain JavaScript object Record<string, ThrottlerStorageOptions> like key value storage. Replacing the plain object with a Map could yield significant performance improvements due to the optimized key-value operations provided by the Map data structure.

Describe the solution you'd like

use Map like key value storage Instead of plain JS object in this place https://github.com/nestjs/throttler/blob/master/src/throttler.service.ts#L11

Teachability, documentation, adoption, migration strategy

proofs: https://www.zhenghao.io/posts/object-vs-map and https://stackoverflow.com/questions/18541940/map-vs-object-in-javascript

What is the motivation / use case for changing the behavior?

because this is a highload service and speeding up read and write operation to storage would be a nice performance improvement

melonges avatar Jun 20 '24 18:06 melonges

I agree with the change. But since ThrottlerStorageService is a public API, we can't change the return type of get storage for now otherwise we'll introduce a breaking change

Just keep in mind that the performance depends on how your app is using it. I saw several benchmarks where access to a plain object were faster. Sometimes Map was faster. Maybe this could change in future releases of v8 idk

micalevisk avatar Jun 20 '24 20:06 micalevisk

I agree with the change. But since ThrottlerStorageService is a public API, we can't change the return type of get storage for now otherwise we'll introduce a breaking change

Just keep in mind that the performance depends on how your app is using it. I saw several benchmarks where access to a plain object were faster. Sometimes Map was faster. Maybe this could change in future releases of v8 idk

Since Map is also an object, it can be written and accessed via the [] operator. The only problem is that the keys written inside ThrottlerStorageService will not be accessible from the outside

melonges avatar Jun 23 '24 18:06 melonges

Would you like to create a PR to address this issue?

micalevisk avatar Jun 23 '24 18:06 micalevisk

@melonges @micalevisk

I once optimized my parser using Map, but I ran into a problem when I exceeded the number of keys (Math.pow(2, 24)). I think you should consider this point (you can look for BigMap).

I'm not sure if it's possible to reach such a limit in such applications, but it's better to keep in mind.

CzarOfScripts avatar Jan 06 '25 05:01 CzarOfScripts