-
Notifications
You must be signed in to change notification settings - Fork 13
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
Why not use more helper functions to eliminate repetition? #68
Comments
While creating a function to encapsulate repetitive code is often a best practice, running functions in JS does have a performance cost. Sometimes it's worth trading nicer looking code for better performance. Also, q5 is made with "vanilla JS", which is a term used to describe plain JS code that doesn't use any libraries and doesn't need to be compiled. |
I'm guessing that macros are out of the pictures then. Do you think that reducing repetition via methods in Vanilla JS is the way to go and something worth investing effort into through PRs though? |
The trigonometric functions can be used hundreds of thousands of times per frame in some sketches, so having an if statement instead of a function call can make a significant difference. Try editing the q5 code as you see fit and run performance tests comparing it and you can see what I mean. Though, you have given me an idea on how to reduce the repetition in the trig functions code: by having loops define the functions. I will test that out soon. |
@RandomGamingDev I added the loops for defining the trig functions in v2.2 I forgot to respond to your question about PRs. I'll certainly accept PRs that improve q5's code quality as long as performance is not degraded. |
I'm going to revert the previous change to the trig functions because doing it the way I did in v2.2 makes them unnamed in performance logs, displaying as |
updated trig functions in v2.3 Line 57 in 8e0224c
|
Repetition of functions that don't need that repetition seems to be common to the point where it's extreme
One example are the trigonmetric functions, for example:
in q5-math.js,
in q5-2d-image.js,
and to a lesser extent
in q5-2d-soft-filters.js
These contain a lot of repetition that could otherwise be reduced to a significantly smaller scale quite easily using vanilla JS and currently take up far too much effort and are too tedious to change, and in general fill up the screen which has many negative ramifications.
While I would understand if this was chosen for performance reasons, this is one of the exact reasons, and one of the largest reasons that p5.js's codebase has become so janky, weighted, and hard to maintain (e.g. p5.js's own p5.Math math library), which is exactly what q5.js is trying to avoid, and is an even bigger issue for q5.js due to its reduced backing compared to p5.js, which has a massive global and corporational backing.
If performance isn't a big issue, I'd recommend using Vanilla JS which has minimal performance impacts, especially with modern JS engines, but if it is, I'd recommend using a Javascript preprocessor or macro library (whichever you choose is up to your goals).
Once a decision is made on whether to remove the repetition or not and how to do so, I'd be happy to create some PRs to help reduce it.
The text was updated successfully, but these errors were encountered: