Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active Sep 27, 2022
Embed
What would you like to do?
charCodeAt vs stringCompare
{"title":"charCodeAt vs stringCompare","initialization":" // 1 2 3 4 5 6 7 8 9 10\n // 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\nconst s = \"the quick brown fox jumps over the lazy dog. [Supercalifragilisticexpialidocious] and the world turns\"\nconst f = \"Supercalifragilisticexpialidocious\"\n\nfunction compareByStringCompare(str) {\n return s.substring(46, 80) === f;\n}\n\nfunction compareByCharCodeAt(str) {\n const len = f.length;\n for (let i = 0; i < len; ++i) {\n if (str.charCodeAt(46 + i) !== f.charCodeAt(i)) {\n return false;\n }\n }\n return true;\n}\n\nfunction test(fn) {\n let sum = 0;\n for (let i = 0; i < 1000; ++i) {\n sum += fn(s) ? 1 : 0;\n }\n}","setup":"// runs before each test","tests":[{"name":"test 1","code":"test(compareByStringCompare)","results":{"aborted":false,"count":2094,"cycles":4,"hz":26220.381429397024,"stats":{"moe":2.4702865480013204e-7,"rme":0.6477185552850311,"sem":1.2603502795925104e-7,"deviation":0.000001016126880721496,"mean":0.000038138270516494025,"variance":1.0325138377247974e-12,"numSamples":65},"times":{"cycle":0.07986153846153848,"elapsed":5.974,"period":0.000038138270516494025,"timeStamp":1664314280722}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36":{"aborted":false,"count":117114,"cycles":6,"hz":1517618.8633841572,"stats":{"moe":3.8184993129911215e-9,"rme":0.5795026587214771,"sem":1.9482139351995516e-9,"deviation":1.582736483000811e-8,"mean":6.589269704845969e-7,"variance":2.5050547746217765e-16,"numSamples":66},"times":{"cycle":0.07716957322133308,"elapsed":6.137,"period":6.589269704845969e-7,"timeStamp":1664313814726}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15":{"aborted":false,"count":1381,"cycles":3,"hz":16866.035218291578,"stats":{"moe":6.548058311463818e-7,"rme":1.1043978209257566,"sem":3.3408460772774585e-7,"deviation":0.0000026517143667065493,"mean":0.00005929075725606682,"variance":7.031589082597915e-12,"numSamples":63},"times":{"cycle":0.08188053577062829,"elapsed":7.408,"period":0.00005929075725606682,"timeStamp":1664314226630}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":2094,"cycles":4,"hz":26220.381429397024,"stats":{"moe":2.4702865480013204e-7,"rme":0.6477185552850311,"sem":1.2603502795925104e-7,"deviation":0.000001016126880721496,"mean":0.000038138270516494025,"variance":1.0325138377247974e-12,"numSamples":65},"times":{"cycle":0.07986153846153848,"elapsed":5.974,"period":0.000038138270516494025,"timeStamp":1664314280722}}}},{"name":"test 2","code":"test(compareByCharCodeAt)","results":{"aborted":false,"count":976,"cycles":2,"hz":7039.307609087629,"stats":{"moe":9.47264498915515e-7,"rme":0.6668086195034565,"sem":4.832982137324056e-7,"deviation":0.0000030566462889705383,"mean":0.00014205942622950822,"variance":9.343086535877363e-12,"numSamples":40},"times":{"cycle":0.13865000000000002,"elapsed":5.956,"period":0.00014205942622950822,"timeStamp":1664314286704}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36":{"aborted":false,"count":976,"cycles":2,"hz":8669.133974833458,"stats":{"moe":5.79202297777211e-7,"rme":0.5021182317962025,"sem":2.955113764169444e-7,"deviation":0.0000020473628726750358,"mean":0.00011535177595628413,"variance":4.1916947324081744e-12,"numSamples":48},"times":{"cycle":0.11258333333333331,"elapsed":5.882,"period":0.00011535177595628413,"timeStamp":1664313820869}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15":{"aborted":false,"count":976,"cycles":2,"hz":9422.94838470108,"stats":{"moe":7.281832371301996e-7,"rme":0.6861633058082418,"sem":3.715220597603059e-7,"deviation":0.000002679083672863559,"mean":0.00010612389659520803,"variance":7.177489326204096e-12,"numSamples":52},"times":{"cycle":0.10357692307692304,"elapsed":6.337,"period":0.00010612389659520803,"timeStamp":1664314234048}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":976,"cycles":2,"hz":7039.307609087629,"stats":{"moe":9.47264498915515e-7,"rme":0.6668086195034565,"sem":4.832982137324056e-7,"deviation":0.0000030566462889705383,"mean":0.00014205942622950822,"variance":9.343086535877363e-12,"numSamples":40},"times":{"cycle":0.13865000000000002,"elapsed":5.956,"period":0.00014205942622950822,"timeStamp":1664314286704}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment