Setting a variable to an existing value compared to returning?

In Javascript, I have a function that sets a variable. If a function tries to set a variable to its current value, is it more "efficient" to break out of the function, or let the function re-set the variable?

Example

var a;
function setStuff(x) {
    if (a == x) { return; }
    a = x;
}

against

var a;
function setStuff(x) {
    a = x;
}

This function will be called when scrolling the page, so it will be called at a high frequency.

+4
source share
5 answers

I wrote a simple test snippet:

var a;
function setStuffCheck(x) {
    if (a == x) { return; }
    a = x;
}

function setStuff(x) {
    a = x;
}

function benchmark(func){
    var startTime = Date.now();
    var callCount = 1000000;

    for(var i = 0; i < callCount; i++){
        func(10);
    }

    console.log((Date.now() - startTime) + "ms for "+callCount+" calls setting always the same value");

    startTime = Date.now();
    for(var i = 0; i < callCount; i++){
        func(i);
    }

    console.log((Date.now() - startTime) + "ms for "+callCount+" calls setting always different values");
}

benchmark(setStuffCheck);
benchmark(setStuff);

By copying and pasting it into the console (Firefox 46.0.1), I have something like this:

138ms for 1000000 calls setting always the same value
216ms for 1000000 calls setting always different values
77ms for 1000000 calls setting always the same value
78ms for 1000000 calls setting always different values

, . . 1 ( 1000, ).

+1

, - "".

, , . , , , , , .

.

var setStuff = function() {
    return newValue;
}

var a = setStuff();
+1

, , , , , .

. , .

0

, , , . , , . , , , , .

, , , :

  • .

, , , . , 10 . 1 10 , , aNew

    var a = 10;
    var ittr = 1000 * 1000;

    function getRandomIntInclusive(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }

    function set1(x){
        if (a === x){ return; }
        a = x;
    }

    function set2(x){
        a = x;
    }

    for (var j = 0; j < 10; j++){
        var start = performance.now();
        for (var i = 0; i< ittr; i++){
            var aNew = a - 10 + getRandomIntInclusive(1,19);
            set1(aNew);
        }
        console.log("conditional : " + (performance.now() - start));
    }

    for (var j = 0; j < 10; j++){
        var start = performance.now();
        for (var i = 0; i< ittr; i++){
            var aNew = a - 10 + getRandomIntInclusive(1,19);
            set2(aNew);
        }
        console.log("unconditional : " + (performance.now() - start));
    }

, () 18 . , , 17.5.

, random(). , 1.8, 18, , , (), , .

0

. :

var a;
function setStuff(x) {
    if (a == x) { return; }
    a = x;
}

setStuffCheck(0) ;
setStuffCheck('0');

console.log(a);

'0', 0.

setStuffCheck ===.

FireFox , . setStuffCheck, , , setStuff, , a, ( ), . 2%, , / , .

In any case, this also means that this small performance difference will depend on how often you expect to call the function with an argument equal to a.

However, the difference will be noticeable only when you make hundreds of millions of calls. If you don't have many calls, then don’t even bother and choose setStuff.

0
source

All Articles