4 ms·
> outer: for (var i = 0, j = 0; i < nlen; i++) { > var nch = needle.charCodeAt(i); > while (j < hlen) { > if (haystack.charCodeAt(j++) === nch) {
by doctorpangloss 11y ago
> outer: for (var i = 0, j = 0; i < nlen; i++) {
> var nch = needle.charCodeAt(i);
> while (j < hlen) {
> if (haystack.charCodeAt(j++) === nch) {
> continue outer;
> }
> }
Oh my god, you can label loops?
- bwaxxlo 11y agoI wanted to actually ask the same thing! I knew of continue but I never encountered such a scenario. It's a poor man's generator.
- arcatek 11y agoIn other languages (such as PHP), the same thing can sometimes be achieved by using "break N" / "continue N", where N is the number of loop you wish to break/continue.
- mgkimsal 11y agoSpecifically in PHP, though, you can't (anymore) break a variable value. break $n; isn't valid since PHP 5.4
- madeofpalk 11y agoWow. That's news to me. I wonder what else is in the JS syntax that I've been missing out on!
- metasean 11y agohttp://blog.scottlogic.com/2015/07/02/surprising-things-about-js.html http://blog.scottlogic.com/2015/07/02/surprising-things-abou...
- nightcracker 11y agoI don't know why the author coded it this way, it can be done much simpler: function fuzzysearch(needle, haystack) { var hlen = haystack.length; var nlen = needle.length; if (nlen > hlen) return false; if (nlen === hlen) return needle === haystack; for (var i = 0, j = 0; i < nlen; ++i, ++j) { while (j < hlen && needle.charCodeAt(i) !== haystack.charCodeAt(j)) ++j; if (j === hlen) return false; } return true; }
- mraleph 11y agoIt's written with continue precisely to avoid additional `if (j === hlen)` check after the loop and having a single increment of `j` in the loop :) To be honest reasoning about the loop with continue is actually easier than about one without.
- joshstrange 11y agoHe says the algorithm was suggested by "Mr. Aleph, a crazy russian compiler engineer working at V8.". It's possible it was suggested to be written this way because V8 (and possibly other interpreters) can optimise it to be faster.
- niix 11y agoYes, but it is typically frowned upon. Here's and MDN on it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- timdorr 11y agoThough in this case, it's a crazy performance hack from Vyacheslav Egorov. Check out the graph: https://cloud.githubusercontent.com/assets/934293/6550014/d3a86174-c5fc-11e4-8334-b2e2b0d38fad.png https://cloud.githubusercontent.com/assets/934293/6550014/d3...
- mraleph 11y agoI would not call it a performance hack, it's just a readable way to write an algorithm - most important part of my original suggestion was actually to use x.charCodeAt(i) instead of x[i]
- joshstrange 11y ago4 spaces and you can get code styling: Like this Or rather outer: for (var i = 0, j = 0; i < nlen; i++) { var nch = needle.charCodeAt(i); while (j < hlen) { if (haystack.charCodeAt(j++) === nch) { continue outer; } } return false; }