KO - computed vs subscription

JavaScript performance comparison

Test case created by vamp

Preparation code

<script type="text/javascript" src="http://knockoutjs.com/downloads/knockout-2.2.1.js"></script>
<script type="text/javascript">
var data = {
        one: "ONE",
        two: "TWO",
        three: "THREE",
        four: "FOUR",
        five: "FIVE",
        six: "SIX",
        seven: "SEVEN",
        eight: "EIGHT",
        nine: "NINE",
        ten: "TEN"
},

firstSource = ko.observable(data),
secondSource = ko.observable(data),

firstData = [],
secondData = [];

for(var i in data){
        if(data.hasOwnProperty(i)){
                firstData.push(ko.computed((function(i){
                        return function(){
                                return firstSource()[i];
                        };
                })(i)));
        }
}

for(var i in data){
        if(data.hasOwnProperty(i)){
                secondSource.subscribe((function(i){
                        var observable = ko.observable(secondSource.peek()[i]);
                        secondData.push(observable);
                        return function(data){
                                observable(data[i]);
                        }
                })(i));
        }
}

function test(observable){
        observable(Object.create(data));
}
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
computed
test(firstSource)
pending…
subscribed
test(secondSource)
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment