PERFORMANCE: calls, direct, apply, bind

Hi ..
I find this result quite surprising but also very interesting.
https://jsperf.com/function-calls-direct-vs-apply-vs-call-vs-bind/6
But following this result, I ask myself an anecdotal question.
I would like to reassure myself that I understood the principle of performance.

I use a lot of hud with lots complex animation, and I find myself having to duplicate inside objects, lots of clone function that this repeats from one object to another.
I would purify the codes, but I am falling on this resulta that left me puzzled.
In my context as an example.

So, example here, I have this.

Code:
//1: OUTSIDE: //EXAMPLE STATUS() ARE IN EATCH OBJ
//WHEN GAME BOOT BUILD THIS
$gameVariables.obj1 = new obj1(NewPid);
$gameVariables.obj2 = new obj2(NewPid);
$gameVariables.obj3 = new obj3(NewPid);
//CONSTRUCTOR
function obj1(NewPid) {
    //EXAMPLE STATUS ARE INSIDE EATCH OBJ
    this.Status = function(){if($gameScreen._pictures[this.Pid()]!==undefined){return true;}return false;}
    this.Pid = function() { return this.NewPid }; // RETURN INITIAL PID
}
function obj2(NewPid) {
    //EXAMPLE STATUS ARE INSIDE EATCH OBJ
    this.Status = function(){if($gameScreen._pictures[this.Pid()]!==undefined){return true;}return false;}
    this.Pid = function() { return this.NewPid }; // RETURN INITIAL PID
}
function obj......(NewPid) {
}
//__________________________________________________________________________________________________
//2: OUTSIDE: //EXAMPLE STATUS() ARE NOT IN EATCH OBJ AND ARE CALLED FROM OUTSIDE
GetStatusInObject = function(){
    if($gameScreen._pictures[this.Pid()]!==undefined){return true;}return false;
}
GetStatusInObject.call(obj1);

The second technique is, therefore, cleaner, but much less performance effective in theory?
So the direct method will remain more logical, even if I have to repeat some cloning functions in the constructor. ?
It is not the weight that disturbs me, but rather the performances for access to information
I don't think the whole RMMV library with 500 plugins could run as 1/10th as slow as all those tests on that site you linked.

I have no idea what it's running, but it seems like it's on an interval or timeout instead of a linear path. The counter for instance would be on the last value and would not go up 1 by 1.
I think it takes the highest and lowest value and then calculates the number of faith that the directives may have been executed.
I know that is a js dev site and all, and I'm a crazy js newb...
delay.gif

but this ^ strikes me as odd...
What is a sample, as slow as that is?
PHP:
while(i < 8554259){ // < is that what that is?
  f.call(that);
  i++;
}

It seems about right though.
Screen Shot 2017-02-24 at 4.17.26 AM.png

The logic is simple I think. Direct is one direct function. Apply and call are technically like two direct functions. I know I'm probably wrong.

It's just that counter as slow as it is is like loading 200 RMMV libraries. I think it's on an interval.
In fact these the number of complete operations per second
So in 1 second Call could be executed 112,758,357 times.
The counter that loop sample, again and again, these to create an average.
Because the same test can give different results.
So the test executes several samples and may take the best average result.

But I think, that seen the difference, I will opt for the most efficient performance solution, but a less clean code.
Efficient is cool, but I don't think anyone notices 1/112,758,357 of a second.
I just wanted to test this kind of loop.
PHP:
// I counted the number of lines in RMMV excluding with regex the comments and lone ends
var pixi = 16552;
var pixiTilemap = 755;
var pixiPicture = 432;
var lzString = 593;
var fpsmeter = 376;
var libTotal = pixi + pixiTilemap + pixiPicture + lzString + fpsmeter;
var core = 4174;
var managers = 1938;
var objects = 7354;
var scenes = 1676;
var sprites = 1930;
var windows = 4065;
var before = (new Date()).getTime();
var after;
var linesTotal = libTotal + core + managers + objects + scenes + sprites + windows;
for(var i = 0; i < linesTotal; i++){
  after = (new Date()).getTime();
}
console.log(linesTotal + " in " + (after - before));
// returns 39845 in 73 milliseconds in Firefox 51.0 Mac OS X 10.12
So roughly the whole RMMV library runs in less than 1/100th of a second.

I'm going to shut up now, because if you are concerned about this level of efficiency, you must be a pro 30,000 lines a day and I'm just a rambling tinkerer who crashes the game at least 2 times per new line in testing.
Don't worry about those kind of micro optimizations, you'll waste time developing and it rarely makes a difference. Maybe if you were creating 10 thousand objects a frame all using apply I doubt you'll have any issues. Then the real question ends up being , why are you making 10 thousand objects a frame.
Why would you not make 10 thousand objects a frame? All the cool kids are doing it.
Why would you not make 10 thousand objects a frame? All the cool kids are doing it.
I just tried it out of curiosity.
PHP:
// don't try this at home
var before = (new Date()).getTime();
var after;
for(var i = 0; i < 10000; i++){
  delete jQuery;
  var jq = document.createElement("script");
  jq.setAttribute("type","text/javascript");
  jq.setAttribute("src","/wp-includes/js/jquery/jquery.js");
  document.head.appendChild(jq);
  after = (new Date()).getTime();
}
console.log("10,000 new jQuery in " + (after - before) + " milliseconds");
// 10,000 new jQuery in 96675 milliseconds
It took 1.6 minutes to reset jQuery 10000 times on my blog, though this might not be accurate because crash prevention kept popping up and I had to click "allow script" a few times.
Interesting. That's actually less time than I would have thought it'd take.

I wonder what the difference would be if you were using apply and call.
Hahaha, all the cools kids like to test limits and @mogwai is one of them 🙂 Interesting results though.
apply 32 matches
call 75 matches
Without crashing the browser I can do 1000 in about 5 seconds.
PHP:
var before = (new Date()).getTime();
var after;
for(var i = 0; i < 1000; i++){
  delete jQuery;
  var jq = document.createElement("script");
  jq.setAttribute("id","jq");
  jq.setAttribute("type","text/javascript");
  jq.setAttribute("src","/wp-includes/js/jquery/jquery.js");
  document.head.appendChild(jq);
  after = (new Date()).getTime();
}
console.log("1,000 new jQuery in " + (after - before) + " milliseconds");
/// 1,000 new jQuery in 5837 milliseconds
I'd say DOM manipulation is a lot more performance heavy than using pixi to create objects in the canvas. You should be able to make a lot more than 1000 objects with Pixi every second.
Hey, he's right. Every 40 milliseconds to be exact.
PHP:
var before = (new Date()).getTime();
var after;
for(var i = 0; i < 1000; i++){
  delete PIXI;
  var px = document.createElement("script");
  px.setAttribute("type","text/javascript");
  px.setAttribute("src","/js/libs/pixi.js");
  document.head.appendChild(px);
  after = (new Date()).getTime();
}
console.log("1,000 new PIXI in " + (after - before) + " milliseconds");
// 1,000 new PIXI in 40 milliseconds

This crashes the game because the game makes calls to PIXI every frame, but I reset it 1,000 times.
Topics like this are where we really separate the casual RM users from the total programming nerds. This is fascinating stuff. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/performance-calls-direct-apply-bind.75262/