tmplat-extension / tmplat

Browser extension that quickly and easily copies info on the current page using a simple and unique template system
https://tmplat.com
MIT License
53 stars 11 forks source link

New Operation: Word Count #193

Closed neocotic closed 11 years ago

neocotic commented 11 years ago

Add a new operation that counts the words within its rendered contents.

neocotic commented 11 years ago

A few things to note on the new wordCount operation;

Finally, I've been testing various methods for counting words and trying to find the best performing solution as this could become a popular tool (e.g. easily count words in the selected text).

I came up with 4 different approaches and tested them first to ensure they worked as expected: http://jsfiddle.net/alasdair/d3KGy/. Then I created a simple performance test to find the best solution and found some surprising results: http://jsperf.com/word-count-test.

Surprisingly, on Chrome at least, the approach with the most code performs best. This is best translation (I think) into CoffeeScript:

count   = 0
text    = text.trim()
matches = text.match /\s+/g

if text
  count++
  count+= matches.length if matches

count
neocotic commented 11 years ago

This has been implemented by PR neocotic/template-chrome#1. One main caveat that I'll possibly fix in the future: special characters are current considered as "words". For example; foo - bar would be counted as having 3 words, as the hyphen would be counted. This is due to the optimized and way in which we're counting words based on spaces between text and words themselves. Also, how am I to know what should be considered a word (e.g !@~# could be considered a - possibly censored - word by some).