While implementing ‘friendly error when user overwrites p5.js functions’ discussed in this issue,
the approach taken was that (solved by this PR) at the start of the run, every function in p5.js is assigned a “binding” by using javascript accessors (i.e, by using getters, setters, by Object.defineProperty) and every time user tries to override the function, the setter function is called and a friendly error is thrown.
code for this lies here
Everything works fine and FES is functioning well.
But, there seems to be a big performance tradeoff by taking this approach.
There seems to be a benchmark test to test the performance of FES here
The times when run were as follows,
FES disabled : 115ms
FES enabled, with this approach : around 280ms
FES enabled, but without using getters and setters : around 120ms
this may not seem like a huge deal, but see the difference between enabling and disabling the FES, it’s only 5ms. all the other workload is only from one simple functionality.
We can overcome this problem by 2 ways:
- We can simply set all the p5.js functions to be unwritable, so no setters required. But, by taking this approach we cannot let the user to use the p5 function’s name as variables, which may not be favoured.
- We should use another approach to reach the same functionality but without using any getters or setters. They are just like attaching a huge number of event listeners imo.
I am trying to solve this problem for the last couple of days but found nothing useful. Hope the community helps.
ps: also while researching about this topic, I stumbled upon many articles regarding why we should not use getters, setters at all in JavaScript, but didn’t find a valuable replacement for them.