title: Word frequency for array of key/values on javascript
body: <p>I'm trying to implement a piece of code on javascript to analyse word/frequency on a given string. My objective is to return a array as the following:</p>
  
  <pre><code>[{text: firstword, size:3 },{text:secondword , size:5 },{text: nword, size: 1},...]
  </code></pre>
  
  <p>I implemented the following code but I'm running out of memory, so I don't really know if its ok or not.</p>
  
  <pre><code>function wordFrequency(txt){
      var wordArray = txt.split(/[ .?!,*'"]/);
      var newArray = [];
      $.each(wordArray, function (ix, word) {
          if (newArray.length &gt;= 1){
              newArray.some(function (w){
                  if (w.text === word){
                      w.size++;
                  } else {
                      newArray.push({text: word, size: 1});
                  }
              });
          } else {
              newArray.push({text: word, size: 1});
          }
      });
      return newArray;
  }
  </code></pre>