简体   繁体   中英

Which one is faster and why ? javascript

So my simple question is : Which is the best solution ( performance and memory wise) in this case :

A. we process the variable into a new one and use it everywhere we need :

    // imagine (a) is an incomming variable that
    // we can't change or else we will break something

    var a = "carrots";

    var b = a.substr(0,a.length-1);
    // and then use b everywhere 

B. we process the variable on the fly:

    // imagine (a) is an incomming variable that
    // we can't change or else we will break something

    var a = "carrots";

    // and then use "a.substr(0,a.length-1);" everywhere 

Please, it's not about how code is written ,is all about which one is faster and better.

Which one you choose and why?

Obviously the first approach is faster. Since we are calling a method and access a property, this is like magnitudes slower than just use/access a resulting string value.

Don't get me wrong, in absolute numbers this still seems like micro optimization, but in this universe and relation it's speed of light vs. a train.

By the way, the concept of storing and caching values in ECMAscript is very commonly used and right so (for different reasons like avoiding scope chains and prototype lookups).

I agree with jAndy.

The first example is going to be faster. You are caching the variable into b so the function you are calling on a is only called once. In your second example everytime you want to use a.substr() you will need to call the function.

So in the first example, you will call a.substr(0, a.length-1) once and cache the result to be used. In example two it will take n times as long where n is the number of times you are calling a.substr(0, a.length-1).

Depending on the what you are doing, there may be no noticeable difference at all. However, the first way is technically faster.

Also, as the comments on jAndy's answer suggest, it is better code since you do not need to repeated call a.substr(0, a.length-1), since you can just cache that.

Agreeing with jAndy. I just wanted to add an example and hopefully give another take.

The first approach is far faster.

Each time we call substring we will need to run through the string and create a new substring object on which to operate on in the future. We must run a method and create a new substring which is clearly more costly.

Think about it this way. You could do something (which costs the same amount as the other option lets say x ) one time, or do this x thing a theoretically unlimited time. You are looking at a cost of x vs infinity.

In theory you are potentially taking up less memory in the second. The var b will hold some memory (depends on the language) in java a string takes up 8 bytes. That will persist until the program closes. Instead if you substring each time you will create a value and then clean it once it is no longer used.

Does that make sense?

We create our var b and keep it around until the program closes (is no longer needed). Or we can create a variable a.substring but remove it when it is no longer needed. Technically depending on how the program works you may have less memory if you had say 100's of these over the entire process. However the memory used is negligible and garbage collection does not occur often enough (in normal conditions) for this to actually be true / relevant.

Memory saving is negligible and time complexity is comparatively significant.

Example A is far superior. Why would you in real life ever do the same thing many times if you can accomplish it by doing it just once?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM