What is it
- Fast - fastest that I can find, often drastically faster... run the tests yourself
- Small - We are talking (431 bytes)
- Portable - Works in 100% of the browsers I've tested on multiple platforms
- Tested - Not everyone writes tests (silly people). Testing using Qunit
- Proper - Passes jslint as well as meets the coding practices and principles of opinionated developers :-)
- Fuzzyable - Optional paramater for fuzziness (allows mismatched info to still score the string)
See it in action
Check it out http://joshaven.com/string_score
(results are for example only... I may change the scoring algorithm without updating examples)
"hello world".score("axl") //=> 0 "hello world".score("ow") //=> 0.35454545454545455 "hello world".score("e") //=>0.1090909090909091 (single letter match) "hello world".score("h") //=>0.5363636363636364 (single letter match plus bonuses for beginning of word and beginning of phrase) "hello world".score("he") //=>0.5727272727272728 "hello world".score("hel") //=>0.6090909090909091 "hello world".score("hell") //=>0.6454545454545455 "hello world".score("hello") //=>0.6818181818181818 ... "hello world".score("hello worl") //=>0.8636363636363635 "hello world".score("hello world") //=> 1 // And then there is fuzziness "hello world".score("hello wor1") //=>0 (the "1" in place of the "l" makes a mismatch) "hello world".score("hello wor1",0.5) //=>0.5581818181818182 (fuzzy) // considers string length 'Hello'.score('h') //=>0.52 'He'.score('h') //=>0.6249999999999999 (better match becaus string length is closer) // Same case matches better then wrong case 'Hello'.score('h') //=>0.52 'Hello'.score('H') //=>0.5800000000000001 // Acronym are given a little more weight "Hillsdale Michigan".score("HiMi") > "Hillsdale Michigan".score("Hills") "Hillsdale Michigan".score("HiMi") < "Hillsdale Michigan".score("Hillsd")
Tested And Operational Under these environments
Fully functional in the 100% of the tested browsers:
- Firefox 3 & 3.5beta (Mac & Windows)
- Firefox 4.0beta (Mac)
- Safari 4 (Mac & Windows)
- Safari 5 (Mac)
- IE: 7 (Windows) **
- Chrome: 2 (Windows)
- Chrome: 9.0 (Mac)
- Opera: 9.64 (Windows)
** IE 7 fails (stop running this script message) with 4000 iterations of the benchmark test. All other browsers tested survived this test, and in fact survive a larger number of iterations. The benchmark that is causing IE to choke is: 4000 iterations of 446 character string scoring a 70 character match.
string_score.js is faster and smaller and does more then either liquidmetal.js or quicksilver.js
The test: 4000 iterations of 446 character string scoring a 70 character match
- Firefox 3.6 (805ms)
- Firefox 4 (245ms)
- Chrome 9 (268ms)
- Safari 5 (259ms)
- Firefox 3.6 (1578ms)
- Firefox 4 (853ms)
- Chrome 9 (339ms)
- Safari 5 (996ms)
- Firefox 3.6 (3300ms)
- Firefox 4 (1994ms)
- Chrome 9 (2835ms)
- Safari 5 (3252ms)
- Firefox 4 (OUCH! I am not sure it heats up my laptop and asks if I want to stop the script... fuzzy_string, nice idea but it doesn't like large strings matches.)
** Tests run with jQuery 1.5 on Mac Book Pro 2.4GHz Core 2 Duo running Snow Leopard *** quicksilver & string_score both use the same test file because they are used in the same way, LiquidMetal has to be called differently so the test file was modified to work with the LiquidMetal Syntax.
string_score.js does not have any external dependencies other then a reasonably new browser.
The tests located in the tests folder relies the files located in the tests folder.
Please share your testing results with me if you are able to test under an unlisted browser.
Thank you Lachie Cox and Quicksilver for inspiration.
Special Thanks to Yesudeep Mangalapilly for further optimizations and establishing the build environment so we can keep the files in sync.
Licensed under the MIT license.