r/javascript • • Mar 17 '20

Javascript Proxies : Real world use cases

https://www.arbazsiddiqui.me/javascript-proxies-real-world-use-cases/
49 Upvotes

19 comments sorted by

4

u/vidarc Mar 17 '20

The article you linked about performance was using node 6 for it's benchmark. Has performance improved since then? Vue 3 will be using proxies I believe and can't imagine they would choose something that would cause a large performance impact

4

u/[deleted] Mar 17 '20

Yes, the linked article is from October 28, 2016. It should not be cited for current performance, and it should be noted that different javascript engines will perform differently. Standards, especially new ones, have a way of drastically improving over time.

And, frankly if 3 million operations a second is too slow for you, then why use JS in the first place to solve that problem?

1

u/lhorie Mar 17 '20

Improved relative to what? Proxies are indeed slow compared to raw property accesses, that hasn't really changed (in fact, with things like hidden classes in the picture, one can expect vanilla property access to be even faster). Vue was using getter/setters under the hood before. I don't expect the perf difference between proxies and getter/setters to have changed much regardless of js engine version. With that said, one does not (hopefully) do O(n) proxy accesses with a huge n value in a typical Vue app, so this would be one of those "but it doesn't matter anyways" kind of cases.

2

u/MyHarvestLife Mar 17 '20

My coworker and I wrote awaitable-array using Proxies to create an Array that can be awaited at a specific length or on a specific condition.

Mainly for use in integration testing: i.e. I want to wait until I get 3 events, then continue my test, etc.

We have a lot of event driven processes, so this helps with testing those use cases, rather than doing bad things like Promise.delay()

Proxies are really useful for tricky things like that - but we don't use this lib in production code for a reason. Not sure how well it would perform under heavy load, we haven't tested it.

1

u/tmarnol Mar 17 '20

Cool! Never thought about the cache possibilites

1

u/yuval_a Mar 17 '20

It is the basis around which the ODM (for MongoDB) I wrote runs on: https://www.npmjs.com/package/derivejs

🙂

1

u/ahmedranaa Mar 17 '20

Informative article and good looking site btw

1

u/[deleted] Mar 18 '20

[removed] — view removed comment

1

u/ahmedranaa Mar 18 '20

What technologies have you used for your blog. Can you give a short overview. and btw is this open source hosted somewhere?

1

u/[deleted] Mar 18 '20

[removed] — view removed comment

1

u/ahmedranaa Mar 18 '20

What kind of database are you using for storing data (blog articles)?

1

u/ahmedranaa Mar 18 '20

What kind of database are you using for storing data (blog articles)?

1

u/R3DSMiLE Mar 17 '20

We used proxies to make a object changes streamable, if you want to get notified when one prop on the object changes you hook to the proxy.events$ which is a simple Subject that emits any set on the object (if you're setting a object to a prop value it will proxy that out as well)

1

u/ssjskipp Mar 18 '20

Immer is an excellent use case for proxies. Turning easy to reason about mutable changes into immutable updates and json patch ops in a way that works with typescript is really nice

1

u/ShitFromMyAss Mar 17 '20

What can be done with proxy objects that can't be done with a regular class?