Live data from Hacker News

JS1k demo: “Highway at Night”

js1k.com

91–100 of 100 posts

Re: JS1k demo: “Highway at Night”

#91
post #58

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...

Almost more importantly in this case, it's less characters adding to the bytecount.

Re: JS1k demo: “Highway at Night”

#92
post #71
post #68

Earlier quoted context omitted.

> 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.

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…

>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”

#93
post #62

Earlier quoted context omitted.

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 kno…

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)

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”

#94
post #13

Earlier quoted context omitted.

I doubt that (all) humans think in javascript :P Im sure the brain has its tricks, but they dont need to have something in common with the ones used here. I wonder what Kolmogorov Complexity this program has. Obviously <= 1023.

How does Kolmogorov Complexity deal with the fact that you're specifying that number within a certain execution context (in this case JavaScript)? Otherwise you could simply reduce the program to a single bit and have an interpreter that includes the original program as a primitive. This program without the libraries that it accesses would be a lot larger than it is. Also, shouldn't the number be expressed in bits ra…

I think it's fair to assume the brain has some interpretation frameworks as good as javascript/html at least, even if it's a cheesy metaphor.

Re: JS1k demo: “Highway at Night”

#95
post #78

Earlier quoted context omitted.

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

Everyone I know uses the + operator, I rarely see others. Funny that I'm not seeing + here.

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”

#96
post #68
post #62

Earlier quoted context omitted.

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 kno…

> 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.

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 `!` to anyway. I'm guessing that the only reason it's used here is character optimization.

Re: JS1k demo: “Highway at Night”

#97

This is so very impressive. I like how it uses canvas to make the moon: // Moon c.fillText("(",99,-99);

You can imagine some designer asking to make the moon look a little different...

This makes you realise how old games must have been driven by the technology.

Re: JS1k demo: “Highway at Night”

#99
post #62
post #58

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...

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 kno…

You could do:

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

    // If exists do...
    if(~someStringOrArray.indexOf(searchTerm))

Re: JS1k demo: “Highway at Night”

#100
post #58

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...

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!

Post reply on HN