Which is the better practice for long scripts?

So, I use a lot of scripts in my game, that have more than the very limited 12 script lines.

Now I wanted to know which of these two options is the better practice?

Option A:
Use a javascript shortener site, shorten the code to a single line and paste it into the script event command. And of course have a readable version in notepad++ or another program.

Option B:
Use a function inside my own plugin and call that function with a script event command.


I have some questions about option B.
1) Does it hit the performance more if the engine needs to find the function inside the plugins instead of just running a one-liner-script?
2) Will it be bad if I have about 1000 lines of different functions and constants in my plugin? Or is the engine fast enough to breeze through the functions and effectively skip all functions that are not currently called without a noticable performance loss?
3) Are functions able to utilize every commands that are also available in script commands or do some of them need parameters passed? Example: event.tileId or something like that.
Option B.

1 - when you call any script code from any game event it uses 'eval' to evaluate the code and then call it. Either way that eval would take place, but making it shorter could only possibly boost efficiency.

2 - there would be no performance loss as long as the code is written efficiently. You could have an 18,000 line long script that has 10000 constants/variables and a few thousands functions or whatever, it makes no difference.

3 - script commands used in the events script command option are evaluated within the scope of an instance of the Game_Interpreter Class. You are able to access any game interpreter function via the 'this' notation. these would not be available unless you passed parameters to your new functions to obtain that information. Things like $gameParty.members() and other global object functions would still be available, cause they are global.

Hope that helps

Edit: you should wrap your plugin code in an anonymous function to ensure that your vaariables and constants are only defined within your code and not made global. For example:

Code:
// global scope
const MyPluginClass = {};

(function(){
  // not global scope
  const ATHING = "Wooo!"
  let anumberforstuff = 0;

  // explicitly create function on MyPluginClass object
  MyPluginClass.afunk = function(param){
    // do stuff...
  };

})();

Further Edit: note that this does make one global object named 'MyPluginClass', but any constants and variables defined within the wrapper function are not made global.
Hope that helps
Actually that helps a lot, thank you!

I'm not sure if I understood 3), though. Does it mean that functions called within an event are treated like the function is run inside the event initially? So all context will be based on the event which called the function? Would it be enough to just pass "this" as a function parameter to have all the events parameters like x and y position etc.?

Is it about reducing occupied memory space to try keeping the own code non global? I would like to have my constants global though, because I like to use $hp and $atk instead of 0 and 2. I can not remember all that numbers, including the numbers of equip slots and movement directions. :LZSjoy:
Moved to: Learning Javascript
Moved to: Learning Javascript
I think it is not nececcarily a javascript question as my main questions are about the RPG Maker MV engine, it's limitations and how effectively it handles the code. But thanks for moving anyhow.
Use are a or b does not really matter, rmmv use `eval("script")` thats have very poor performance.
Sure B are just little more fast, because js have less code to parse and eval.
If you use only scripts commands in loop you will destroy cpu of your user :troll:.

You should avoid use command script to eval scripts.
Is better to create your plugin with listeners and events.

all dev will tell you.
Don’t use ‘eval’
Although the “eval” function is a good method to run arbitrary code, each string that is passed to the eval function has to be parsed and executed on-the-fly. This cost has to be paid every time the execution reaches an eval function call.
Ok thank you. It is good to know. Unfortunately my javascript skills are not good enough yet to create my own plugins with listeners. I'll stick to calling functions for now. :LZScheeze:
Like everyone said, option B is better. Eval is not very efficient.

Original thread: https://forums.rpgmakerweb.com/threads/which-is-the-better-practice-for-long-scripts.110752/