Ways to speed up eval() ?

peq42_

Yeet
Veteran
Joined
Feb 5, 2016
Messages
484
Reaction score
288
First Language
Portuguese(BR)
Primarily Uses
RMMV
What are the best ways to speed up the execution of code inside a string with eval()?

I know most people say that eval allows for serious code-breaking problems, but since i'm left with no alternative(new function('string') doesn't work here because of the lack of access to certain things) I need to gather as much information as I can on this.


I heard(and confirmed on tests) that adding a function before the string and then calling it(like this: "myfunction=function(){"+thestring+"}myfunction()" and then evall all that) speed ups by up to 4x the performance, but I still wonder if there's more I can do, since it's still 10x slower than executing it as normal code
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
perhaps somehow create a cache of functions that were created by eval, and then call that function instead of re-calling the eval? the initial eval would be the only one actually evall-ing. :)

That said, dont eval if you can avoid it. Sometimes you cant, it happens. But the best way to speed up the execution of your code is to not use eval at all. Try think of a way around what your trying to achive. :)
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,431
Reaction score
7,712
First Language
German
Primarily Uses
RMMV
And if you tell us what exactly you want to do, we might point you to workarounds...
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
1. Is the string defined when the game is booted (i.e. from the database) or is the string determined dynamically during runtime?

but since i'm left with no alternative(new function('string') doesn't work here because of the lack of access to certain things)
2. What is "certain things", exactly? I use the function constructor plenty for this sort of thing.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,640
First Language
Czech
Primarily Uses
RMMV
Apart from optimizing the function you really can't. The code is getting compiled on the go, meaning it's not in our hands, that is unless you understand the code of Chromium and can optimise the compiler.
Unless you're building 3rd party plugins that aim for a ton of customizability, oftentimes you can avoid the eval though.
 

armornick

Villager
Member
Joined
Sep 1, 2017
Messages
19
Reaction score
4
First Language
Dutch
Primarily Uses
RMMV
A general alternative to eval, assuming you know ahead of time which variables the formula can use, is something like the following:

Code:
var fn = new Function("a", "b", "return " + formula);
Afterward, you just call it with the variables and it should work. The problem is that eval lets you use all of the local variables declared in the context where the eval is called so caching the execution in a function means you have to restrict which variables your formulas can use.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,040
Messages
1,018,470
Members
137,821
Latest member
Capterson
Top