Underscore.js vs map, reduce, keys vs Low level

JavaScript performance comparison

Revision 3 of this test case created by and last updated

Preparation code

<script src="https://rawgithub.com/jashkenas/underscore/ff32da25c2bf0a6ea1a8e9b7bda3651b1de601c2/underscore.js"></script>
<script src="https://rawgithub.com/lodash/lodash/6f31654f5ce025af9fe6560f976c9984827267b0/dist/lodash.js"></script>
<script>
var lodash = _.noConflict();
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var text = "Alice was beginning to get very tired of sitting by her sister on the \
  bank, and of having nothing to do: once or twice she had peeped into the \
  book her sister was reading, but it had no pictures or conversations in \
  it, 'and what is the use of a book,' thought Alice 'without pictures or \
  conversations?'\
  \
  So she was considering in her own mind (as well as she could, for the \
  hot day made her feel very sleepy and stupid), whether the pleasure \
  of making a daisy-chain would be worth the trouble of getting up and \
  picking the daisies, when suddenly a White Rabbit with pink eyes ran \
  close by her.";

};
</script>

Preparation code output

<script src="https://rawgithub.com/lodash/lodash/6f31654f5ce025af9fe6560f976c9984827267b0/dist/lodash.js"></script> <script> var lodash = _.noConflict(); </script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Low level
function textWords(text) {
    var words = text.match(/[a-zA-Z\-]+/g);

    for (var i = 0; i < words.length; i++) {
        words[i] = words[i].toLowerCase();
    }
    return words;
}

function wordsFrequencies(words) {
    var frequencies = {},
        currentWord = null;
    
    for(var i = 0; i < words.length; i++) {
        currentWord = words[i];
        frequencies[currentWord] = (frequencies[currentWord] || 0) + 1;
    }
    return frequencies;
}

function sortedListOfWords(wordsFrequencies) {
    var words = [];

    for (var key in wordsFrequencies) {
        if (wordsFrequencies.hasOwnProperty(key)) {
            words.push(key);
        }
    }
    return words.sort();
}

function topTenWords(wordsFrequencies) {
    var frequencies = [],
        result = [];

    for (var key in wordsFrequencies) {
        if (wordsFrequencies.hasOwnProperty(key)) {
            frequencies.push([key, wordsFrequencies[key]]);
        }
    }

    frequencies = frequencies.sort(function(freq1, freq2) {
        return freq1[1] < freq2[1] ? 1 : (freq1[1] > freq2[1] ? -1 : 0);
    });

    for (var i = 0; i < 10; i++) {
        result[i] = frequencies[i][0];
    }
    return result;
}

function analyzeText(text) {
    var words = textWords(text),
        frequencies = wordsFrequencies(words),
        used = sortedListOfWords(frequencies),
        topTen = topTenWords(frequencies);
}

analyzeText(text);
pending…
map, reduce, keys
function textWords(text) {
    return text.match(/[a-zA-Z\-]+/g).map(function(word) {
        return word.toLowerCase();
    });
}

function wordsFrequencies(words) {
    return words.reduce(function(frequencies, word) {
        frequencies[word] = (frequencies[word] || 0) + 1;
        return frequencies;
    }, {});
}

function sortedListOfWords(wordsFrequencies) {
    return Object.keys(wordsFrequencies).sort();
}

function topTenWords(wordsFrequencies) {
    var frequencies = [],
        result = [];

    frequencies = Object.keys(wordsFrequencies).map(function(key) {
        return [key, wordsFrequencies[key]];
    }).sort(function(freq1, freq2) {
        return freq1[1] < freq2[1] ? 1 : (freq1[1] > freq2[1] ? -1 : 0);
    });

    for (var i = 0; i < 10; i++) {
        result[i] = frequencies[i][0];
    }
    return result;
}

function analyzeText(text) {
    var words = textWords(text),
        frequencies = wordsFrequencies(words),
        used = sortedListOfWords(frequencies),
        topTen = topTenWords(frequencies);
}

analyzeText(text);
pending…
Underscore.js
function textWords(text) {
    return _.map(text.match(/[a-zA-Z\-]+/g), function(word) {
        return word.toLowerCase();
    });
}

function wordsFrequencies(words) {
    return _.reduce(words, function(frequencies, word) {
        frequencies[word] = (frequencies[word] || 0) + 1;
        return frequencies;
    }, {});
}

function sortedListOfWords(wordsFrequencies) {
    return _.keys(wordsFrequencies).sort();
}

function topTenWords(wordsFrequencies) {
    var frequencies = [],
        result = [];

    frequencies = _.map(_.keys(wordsFrequencies), function(key) {
        return [key, wordsFrequencies[key]];
    }).sort(function(freq1, freq2) {
        return freq1[1] < freq2[1] ? 1 : (freq1[1] > freq2[1] ? -1 : 0);
    });

    for (var i = 0; i < 10; i++) {
        result[i] = frequencies[i][0];
    }
    return result;
}

function analyzeText(text) {
    var words = textWords(text),
        frequencies = wordsFrequencies(words),
        used = sortedListOfWords(frequencies),
        topTen = topTenWords(frequencies);
}

analyzeText(text);
pending…
Lo-Dash
function textWords(text) {
    return lodash.map(text.match(/[a-zA-Z\-]+/g), function(word) {
        return word.toLowerCase();
    });
}

function wordsFrequencies(words) {
    return lodash.reduce(words, function(frequencies, word) {
        frequencies[word] = (frequencies[word] || 0) + 1;
        return frequencies;
    }, {});
}

function sortedListOfWords(wordsFrequencies) {
    return lodash.keys(wordsFrequencies).sort();
}

function topTenWords(wordsFrequencies) {
    var frequencies = [],
        result = [];

    frequencies = lodash.map(lodash.keys(wordsFrequencies), function(key) {
        return [key, wordsFrequencies[key]];
    }).sort(function(freq1, freq2) {
        return freq1[1] < freq2[1] ? 1 : (freq1[1] > freq2[1] ? -1 : 0);
    });

    for (var i = 0; i < 10; i++) {
        result[i] = frequencies[i][0];
    }
    return result;
}

function analyzeText(text) {
    var words = textWords(text),
        frequencies = wordsFrequencies(words),
        used = sortedListOfWords(frequencies),
        topTen = topTenWords(frequencies);
}

analyzeText(text);
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.

0 Comments