Drakkonis

Veteran
Veteran
Joined
Mar 5, 2021
Messages
210
Reaction score
160
First Language
English
Primarily Uses
RMMZ
Vague title, I know. Been awhile since I've posted here. Ignore the prefix, the question concerns javascript itself.

I have a bit of code. This bit of code will occur every frame update, hence why I ask which approach is faster.

In this code I have a formula that requires getting a value from a property. Stepping through the code reveals that grabbing this property's value jumps through two other points in the code. There are two of these properties I'm grabbing, and the values are used twice each.

So here's the question. Is it faster to simply grab the property values directly both times, or grab it once to store it in a variable, and then use the variable itself those two times? Is there any real appreciable difference in speed, even if I were to drastically scale up the number of times I used said property value? Or even the number of similar properties I'm accessing each frame?

I don't INTEND to go that massive, but the project I'm working on is a plugin with a great deal of high-degree customization options for the user, so I'd like to know the more time-efficient method just in case someone decides to get.... stupidly creative. :p

There won't be a great deal of such access in any one function, but there isn't a limit to a chain of such functions that a user could theoretically implement.

I've not really delved very far into the topic of time complexity or memory usage, and even cursory inspection on the topics usually implies a LARGE amount of a given operation being done in sequence. Given the frequency with which I'm going to be potentially running one of any number of defined functions, I figured I'd see if there was a definite advantage to one approach over another before I started really getting into writing all those functions.
 

OcRam

Servant of the Universe
Veteran
Joined
Aug 2, 2016
Messages
768
Reaction score
891
First Language
Finnish
Primarily Uses
RMMZ
Vague title, I know. Been awhile since I've posted here. Ignore the prefix, the question concerns javascript itself.

I have a bit of code. This bit of code will occur every frame update, hence why I ask which approach is faster.

In this code I have a formula that requires getting a value from a property. Stepping through the code reveals that grabbing this property's value jumps through two other points in the code. There are two of these properties I'm grabbing, and the values are used twice each.

So here's the question. Is it faster to simply grab the property values directly both times, or grab it once to store it in a variable, and then use the variable itself those two times? Is there any real appreciable difference in speed, even if I were to drastically scale up the number of times I used said property value? Or even the number of similar properties I'm accessing each frame?

I don't INTEND to go that massive, but the project I'm working on is a plugin with a great deal of high-degree customization options for the user, so I'd like to know the more time-efficient method just in case someone decides to get.... stupidly creative. :p

There won't be a great deal of such access in any one function, but there isn't a limit to a chain of such functions that a user could theoretically implement.

I've not really delved very far into the topic of time complexity or memory usage, and even cursory inspection on the topics usually implies a LARGE amount of a given operation being done in sequence. Given the frequency with which I'm going to be potentially running one of any number of defined functions, I figured I'd see if there was a definite advantage to one approach over another before I started really getting into writing all those functions.
Hi,

Properties CAN be SUPER slow - it depends quite a lot of the logic behind said property.

Example in this code below it's almost TWICE slower to read property two times compared to const. And actually logic is really simple... Can't even imagine difference if there would be more complex logic behind the property!
Code:
obj = {}; obj.a = 1;
Object.defineProperty(obj, "something", {
    get: function() {
        return (this.a == 2) ? "It's two" : (this.a > 2) ? "Greater than 2" : "Less than 2";
    }
});

let started = Date.now();
for (let i = 0; i < 100000; i++) {
if (obj.something == "It's two") {
} else if (obj.something == "Anything") {}
}
console.log("Accessed by property:" + (Date.now() - started));

started = Date.now();
for (let i = 0; i < 100000; i++) {
const tmp = obj.something;
if (tmp == "It's two") {
} else if (tmp == "Anything") {}
}
console.log("Accessed by const:" + (Date.now() - started));

EDIT: Single "let" is slightly faster than using const in loop (at least in this case)
Code:
let tmp = "";
for (let i = 0; i < 100000; i++) {
tmp = obj.something;
if (tmp == "It's two") {
} else if (tmp == "Anything") {}
}
 
Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
33,725
Reaction score
8,509
First Language
German
Primarily Uses
RMMV
I suggest you approach it from a different direction:
how often do the values really change?

if they rarely change it might be better to store the value in a variable with quick access, or only process the calculation after a change instead of going through them every update.

that is how most useless calculations are cut off - not by optimizing them but by only doing them if they are really needed.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,756
Reaction score
2,851
First Language
English
Primarily Uses
RMMZ
As the others have said, this largely depends on what the function you're getting the property from is actually doing.

My general rule of thumb is to reference properties directly if I'm only using them once in a given function, or store them in a shorter identifier if I'm using them twice or more.
 

Drakkonis

Veteran
Veteran
Joined
Mar 5, 2021
Messages
210
Reaction score
160
First Language
English
Primarily Uses
RMMZ
Thanks all for the replies!

In the specific instance I'm working on, the properties I'm grabbing are a window's x and y coordinates. The property is an alias for position.x and position.y, which themselves are linked to the window's transform object/property/whatever it actually is. For the most part, no additional processing is being done, merely jumping through the hoops to get the actual value of the property. I could cut one jump out entirely by using transform.x/transform.y, but I'm using window.x and window.y in the first place to type less code over and over again.

But it's merely one example, I know that grabbing its dimensions are also going to be required. Eventually I might have to grab even more things in that vein, but off the top of my head I'm not 100% sure about that. This evolves more and more as I try to get my original goal with it completed.

In either event, I wasn't sure how speed would be affected with such simple code jumps being repeated. Normally I wouldn't be concerned about it, but as this is a "potentially hundreds of these operations occurring every frame" scenario, I'd rather cut time down on the ground floor, since the potential wait time could grow exponentially, eventually causing enough delay to lag. Not sure how much that would actually take, but I'd rather mitigate that risk as much as possible when/where I can.
 

Latest Threads

Latest Profile Posts

Worked on a cutscene skip domino effect using Maker Systems' script as the basis. Basically if I have a batch of cutscenes in a row on different maps, now when you skip one cutscene it will skip the entire set. No more spamming X to skip a scene every new map.

Oh and it will work regardless of where you do it from in the set. Convenient!
seems absolutely WILD to me now all the hundreds of hours of work I used to do in 2k(3) over a decade ago with a resolution of 320x240. that seems so insanely TINY now, seemed so normal at the time!
Playing Aroma Indosiar theme song.
Aroma is an Indonesian TV show about cooking. :)
Working on a characteeer! (Why is it so hard to color hair?) ;-;
Silly-Sketches.png
Just finished eventing a couple simple puzzles in RMMV, for my very first game "Sample Quest." Just messing around, throwing things together, and calling it a story XD On a non-RM note, got my hair cut today!

Forum statistics

Threads
115,379
Messages
1,089,395
Members
150,079
Latest member
Fencis
Top