- Joined
- Mar 24, 2012
- Messages
- 13
- Reaction score
- 3
- First Language
- English
- Primarily Uses
- N/A
Conditional branches are one example of where the engine can call Javascript code provided by the user. They're processed by the function Game_Interpreter.prototype.command111 at lines 9183 to 9318 in rpg_objects.js, and lines 9306 to 9308 of that function handle the case where the condition must evaluate Javascript code. They read:
But that code calls eval on a string containing Javascript code every time the conditional branch is executed, and eval in Javascript is noted for being slow. It appears that it could be replaced with the following code, which will cause the eval call to be made only once per map visit:
I've tested this with a simple condition, and it seems to work, but are any performance improvements likely? Or is this likely to break horribly in a way that I've overlooked?
Code:
case 12: // Script
result = !!eval(this._params[1]);
break;
Code:
case 12: // Script
if (typeof(this._params[1]) === 'string') {
eval('this._params[1] = () => ' + this._params[1]);
}
result = !!this._params[1]();
break;

