Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function SpeedComparison(testData, accuracy, methods) {
    if ( ! (this instanceof arguments.callee) ) { return; }
    this.accuracy = accuracy || 1000;
    this.methods = methods || {};
    this.testData = testData;
    this.results = {};
}

SpeedComparison.prototype = {
    addMethod : function(name, method) {
        this.methods[name] = method;
        return this;
    },
    removeMethod : function(name) {
        delete this.methods[name];
        return this;
    },
    begin : function() {
        iterateMethods : for (var method in this.methods) {
            var sTime = +new Date();
            for (var i = 0, a = this.accuracy; i < a; i++) {
                if (this.methods[method](this.testData) === false) {
                    this.results[method] = 'Failed';
                    continue iterateMethods;
                }
            }
            var fTime = +new Date();
            this.results[method] = fTime - sTime;
        }
    }
};



var test = new SpeedComparison(['a','really','long','array'], 1000);

// Add each method to be tested:
test.methods = {
    'STRING' : function(arr) {
        var built = [];
        for (var i = 0, l = arr.length; i < l; i++) {
            built[built.length] = '&lt;li&gt;' + arr[i] + '&lt;/li&gt;';
        }
        NN.push(built.join(''));
    },
    'ARRAY' : function(arr) {
        var list = '';
        for (var i = 0, l = arr.length; i < l; i++) {
            list += '&lt;li&gt;' + arr[i] + '&lt;/li&gt;';
        }
        NN.push(list);
    },
    'JOIN()' : function(arr){
        NN.push(arr.join('&lt;/li&gt;&lt;li&gt;'));
    }
};


// Log results
log(test.results);
// Assert all methods worked as expected:
log(NN[0] === NN[1] && NN[1] === NN[2]);