Word count-exercism (v2)

Revision 2 of this benchmark created by Dan on


Description

exercism.io word count

Setup

var phrase = "sed fermentum orci consectetur nisi interdum porttitor. pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. aliquam semper nisl luctus, semper sem a, aliquet nisi. nunc viverra sem dignissim turpis dictum, eget lacinia nibh adipiscing. aenean eget bibendum felis. in eleifend viverra egestas. fusce eu pellentesque metus, sed interdum dolor. maecenas interdum libero nec ante ultrices hendrerit. nunc varius placerat metus in scelerisque. etiam vestibulum quam nisi, sed eleifend risus placerat in. proin lobortis bibendum eros, vel cursus dolor aliquam eget. nam ullamcorper scelerisque accumsan. nunc ut eleifend nisi. mauris ac orci mi. nulla facilisi. praesent tristique malesuada placerat.";

Test runner

Ready to run.

Testing in
TestOps/sec
while
var count = {};

var matches = phrase.match(/\w+/gi);
var len = matches.length - 1;

while (len >= 0) {
  word = matches[len];
  if (count[word] == undefined) {
    count[word] = 1;
  } else {
    count[word]++;
  }
  len--;
}
ready
forEach
var count = {};

var matches = phrase.match(/\w+/gi);

matches.forEach(function(word) {
  if (count[word] == undefined) {
    count[word] = 1;
  } else {
    count[word]++;
  }
});
ready
mine
var numbers = {};
var matches = phrase.match(/\b\S+\b/g);
matches.forEach(function(word) {
  //word = word.toLowerCase();
  if (numbers[word] === undefined)
    numbers[word] = 1;
  else
    numbers[word]++;
});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Tarcísio Gruppi on
  • Revision 2: published by Dan on