News for hackers. Stuff that matters.
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by doctorpangloss on Monday July 20, 2015 @06:21AM (#9915030)
> 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?

Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by madeofpalk on Monday July 20, 2015 @06:27AM (#9915057)
Wow. That's news to me.

I wonder what else is in the JS syntax that I've been missing out on!

Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by joshstrange on Monday July 20, 2015 @12:28PM (#9917126)
4 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;
      }
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by metasean on Monday July 20, 2015 @09:12AM (#9915761)
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by nightcracker on Monday July 20, 2015 @10:16AM (#9916157)
I 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;
    }
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by joshstrange on Monday July 20, 2015 @12:26PM (#9917110)
He 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.
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by mraleph on Monday July 20, 2015 @10:40AM (#9916337)
It'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.

Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by niix on Monday July 20, 2015 @10:37AM (#9916313)
Yes, but it is typically frowned upon. Here's and MDN on it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by timdorr on Monday July 20, 2015 @11:30AM (#9916665)
Though in this case, it's a crazy performance hack from Vyacheslav Egorov. Check out the graph: https://cloud.githubusercontent.com/assets/934293/6550014/d3...
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by mraleph on Monday July 20, 2015 @11:41AM (#9916742)
I 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]
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by bwaxxlo on Monday July 20, 2015 @06:27AM (#9915056)
I 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.
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by arcatek on Monday July 20, 2015 @07:25AM (#9915244)
In 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.
Re:Fuzzysearch: Tiny, fast fuzzy searching for JavaScript by mgkimsal on Monday July 20, 2015 @09:39AM (#9915927)
Specifically in PHP, though, you can't (anymore) break a variable value.

break $n;

isn't valid since PHP 5.4