News for hackers. Stuff that matters.
Re:JS1k demo: “Highway at Night” by ape4 on Sunday November 23, 2014 @12:31PM (#8649269)
I saw the double tilde (~~) in the code and wondered what it was. Of course, single tilde is the bitwise not operator. Googling finds that double tilde is a faster Math.floor(). http://stackoverflow.com/questions/5971645/what-is-the-doubl...
Re:JS1k demo: “Highway at Night” by nmjohn on Sunday November 23, 2014 @01:04PM (#8649363)
Learning about bitwise operators (and everything they entail - so binary in general, endianness, etc) was without a doubt the most beneficial thing I've ever done for my programming abilities.

It was a complete eye opener to how computers work on a far more fundamental level than any other aspect of programming had previously revealed to me.

For anyone without a traditional CS educational background, if you don't know much about bitwise operations or binary, I highly recommend sitting down and spending a few hours exploring them - it will make you think about certain aspects of programming in a very different manner, which - at least for me - resulted in writing much better code.

Aside: While the double tilde is handy, I use the single tilde operator far more often in javascript. If you precede an indexOf statement with a tilde, it essentially turns `indexOf` into `contains`.

    if(~someStringOrArray.indexOf(searchTerm))
It works because indexOf returns the index the term is found at, or -1 if it isn't found. This means 0 is actually a successful result, but Boolean(0) = false. But bitwise NOT will clear all of that up:

    ~(-1) = 0 (evaluates as false)
    ~(0) = -1 (evaluates as true)
Edit: I'm not suggesting everyone go off and use ~indexOf all the time - I was simply sharing a related javascript bitwise "trick".
Re:JS1k demo: “Highway at Night” by zoul on Sunday November 23, 2014 @01:32PM (#8649441)
I would probably hate you for not writing plain “!=-1” instead.
Re:JS1k demo: “Highway at Night” by serve_yay on Sunday November 23, 2014 @07:58PM (#8650563)
No kidding, "clever" stuff like that drives me nuts.
Re:JS1k demo: “Highway at Night” by grecy on Sunday November 23, 2014 @01:52PM (#8649504)
> if(~someStringOrArray.indexOf(searchTerm))

Do you think the person maintaining your code 2-5 years from now will understand that easily and quickly?

For maintenance, it's important to be as clear as possible, not as tricky as possible.

Re:JS1k demo: “Highway at Night” by nmjohn on Sunday November 23, 2014 @02:13PM (#8649562)
Which is why I only use it on solo projects.

That being said, ~indexOf is hardly tricky. if(string.indexOf(term)) is far trickier, because if you didn't realize it returning 0 was a positive result, you easily could write it without a -1 check and not notice it during testing if no values had an index of 0. Then months later you would have a hell of a time debugging it because it would look so innocuous.

On the other hand, by including the tilde, you ensure two things: 1.) Future developer knows what it does and has no problems. 2.) Future developer hasn't seen it before and goes straight to google to see what it does - at which point they learn and now will recognize it in the future.

So at the end of the day, by using a ~, it is explicitly clear that indexOf() should not be used directly as a coerced boolean, and it ensures that anyone touching the code will either know or learn why.

Re:JS1k demo: “Highway at Night” by vegedor on Sunday November 23, 2014 @09:18PM (#8650736)
>On the other hand, by including the tilde, you ensure two things: 1.) Future developer knows what it does and has no problems

The premises was that the future dev won't know. It was hard to write, it should be hard to read moderately applies, I guess.

Re:JS1k demo: “Highway at Night” by zamalek on Monday November 24, 2014 @02:46AM (#8651423)
If you come from a C background you would probably parse the intent of that immediately. Bitwise complement is pretty basic stuff and is probably what the C compiler winds up optimizing `!<int>` to anyway. I'm guessing that the only reason it's used here is character optimization.
Re:JS1k demo: “Highway at Night” by tlrobinson on Sunday November 23, 2014 @01:38PM (#8649466)
You definitely should learn bitwise operators, but if other people have to work with your code I'm not generally in favor of applying them in clever ways where there's a slightly more verbose but much more readable solution (e.x. "~x.indexOf(y)" vs "x.indexOf(y) !== 0"... just type the extra 3 characters)
Re:JS1k demo: “Highway at Night” by juhq on Sunday November 23, 2014 @03:34PM (#8649839)
As a rule of thumb, when ever one thinks that "wow, this code I just wrote is really clever", it should be a sign to sit back, take another approach and rewrite that code.

Clever code almost always means that in hindsight it's terrible and will introduce bugs.

In my opinion "x.indexOf(y) !== 0" is a lot more clearer and will immediately tells other developers what is happening. No point in saving space code, that's why we have minification libraries.

Re:JS1k demo: “Highway at Night” by lstamour on Sunday November 23, 2014 @08:35PM (#8650647)
Nobody needs this correction, I hope, but to be clear, it's !== -1 that we're replacing with ~. Then again, maybe this stands as a valid reason to be explicit in the first place. ;-)
Re:JS1k demo: “Highway at Night” by tracker1 on Sunday November 23, 2014 @09:24PM (#8650763)
A perfect example of why "~x.indexOf(y)" may actually be better... you just messed up, as the alternative should be "x.indexOf(y) !== -1" or "x.indexOf(y) >= 0" or "x.indexOf(y)+1" ...

In any case, it's all a matter of style... because if you don't know that indexOf will return 0 for a positive match, you will still mess up.

Re:JS1k demo: “Highway at Night” by Cthulhu_ on Sunday November 23, 2014 @01:51PM (#8649501)
Re:JS1k demo: “Highway at Night” by nmjohn on Sunday November 23, 2014 @02:04PM (#8649528)
> This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.

It only works in firefox and if I remember correctly, it is no longer part of the latest ECMA 6 spec.

Re:JS1k demo: “Highway at Night” by ttty on Monday November 24, 2014 @04:31AM (#8651611)
You could do:

    var exists = ~someStringOrArray.indexOf(searchTerm);
    if(exists){...}
or

    // If exists do...
    if(~someStringOrArray.indexOf(searchTerm))
Re:JS1k demo: “Highway at Night” by imaginenore on Sunday November 23, 2014 @05:33PM (#8650198)
And that's how you create unreadable code.

Things like that are only acceptable if you're optimizing a bottleneck.

Re:JS1k demo: “Highway at Night” by chrismorgan on Sunday November 23, 2014 @05:50PM (#8650253)
~x.indexOf(…) would be “starts with” rather than “contains”, for negative numbers such as -2 (from ~1) are truthy.
Re:JS1k demo: “Highway at Night” by nmjohn on Sunday November 23, 2014 @07:44PM (#8650541)
> ~x.indexOf(…) would be “starts with” rather than “contains”, for negative numbers such as -2 (from ~1) are truthy.

Nope, here's an example you can run it run in your console:

    function contains(needle, haystack) {
      return Boolean(~haystack.indexOf(needle));
    }
    
    var haystack = "The quick brown fox jumps over the lazy dog";
    console.log(contains("The", haystack));
    console.log(contains("dog", haystack));
    console.log(contains("brown fox jumps", haystack));
Here are three potential cases:

    haystack.indexOf('dog') = 40 -> truthy
    ~40 = -41 -> truthy
    
    haystack.indexOf('The') = 0 -> falsey (WRONG)
    ~0 = -1 -> truthy (FIXED)
    
    haystack.indexOf('nope') = -1 -> truthy (WRONG)
    ~-1 = 0 -> falsey (FIXED)
Re:JS1k demo: “Highway at Night” by Igglyboo on Sunday November 23, 2014 @09:14PM (#8650722)
indexOf will only return -1, 0, or a positive integer
Re:JS1k demo: “Highway at Night” by jhgg on Sunday November 23, 2014 @03:36PM (#8649848)
It's a bit more than that. It can also be used to convert strings (containing numeric values) to Numbers. More commonly seen than the prefix "~~" is "|0", because of it's low order precedence.

    > '15'
    '15'
    > ~~'15'
    15
    > '15'|0
    15
    > 15.99|0
    15
    > ~~15 * 1.3
    19.5
    > 15 * 1.3 | 0
    19
Re:JS1k demo: “Highway at Night” by owenversteeg on Sunday November 23, 2014 @08:52PM (#8650679)
Everyone I know uses the + operator, I rarely see others. Funny that I'm not seeing + here.
Re:JS1k demo: “Highway at Night” by tracker1 on Sunday November 23, 2014 @09:30PM (#8650783)
With ~~foo you will always get an integer that fits inside a 32-bit integer range... regardless of the input... invalid numbers will be 0. Whereas with a +foo you may get NaN (for an invalid decimal input).

Part of this is because bitwise operations will convert whatever the input value is to a 32-bit integer value before performing the operation, and not fail (invalid input becomes 0 opposed to NaN).

Re:JS1k demo: “Highway at Night” by z3t4 on Monday November 24, 2014 @05:46AM (#8651809)
I did some testing a while ago in V8 engine and didn't see any performance benefits using bitwise operations. It seems the engine do these optimizations anyway!

I wouldn't recommend anyone doing optimizations in especially JavaScript unless it's really needed. Remember that optimizations are the root of all evil!

Re:JS1k demo: “Highway at Night” by vegedor on Sunday November 23, 2014 @09:15PM (#8650728)
Almost more importantly in this case, it's less characters adding to the bytecount.