Live data from Hacker News

JS1k demo: “Highway at Night”

js1k.com

71–80 of 100 posts

Re: JS1k demo: “Highway at Night”

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

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”

#72
post #3

The making of "Highway at Night": http://jsriffs.blogspot.fi/2014/05/making-of-highway-at-nigh...

The visual trickery is fun. I hadn't noticed that only the centre lines on the road glow until reading that, nor had I noticed that the street lights don't actually have a thin line linking the pole and the light.

I don't think the street lights are supposed to be on poles. It appears to be a tunnel with lights mounted overhead.

Re: JS1k demo: “Highway at Night”

#73

Earlier quoted context omitted.

The visual trickery is fun. I hadn't noticed that only the centre lines on the road glow until reading that, nor had I noticed that the street lights don't actually have a thin line linking the pole and the light.

I don't think the street lights are supposed to be on poles. It appears to be a tunnel with lights mounted overhead.

But they're called street lights and they aren't in the tunnel!

Re: JS1k demo: “Highway at Night”

#74

Earlier quoted context omitted.

The visual trickery is fun. I hadn't noticed that only the centre lines on the road glow until reading that, nor had I noticed that the street lights don't actually have a thin line linking the pole and the light.

I don't think the street lights are supposed to be on poles. It appears to be a tunnel with lights mounted overhead.

Let it run longer. There are at least 4 distinct sections, including a city.

Re: JS1k demo: “Highway at Night”

#75

Earlier quoted context omitted.

I don't think the street lights are supposed to be on poles. It appears to be a tunnel with lights mounted overhead.

But they're called street lights and they aren't in the tunnel!

Ha! Sorry, clearly I didn't let it run long enough. I closed it because it lags pretty hard for me.

Re: JS1k demo: “Highway at Night”

#77
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)

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”

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

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”

#79
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…

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”

#80
post #21

Now, I remember that in a recent discussion there was some arguing on Firefox new JS engine whose results were benchmarked at a higher position than Chroome's. So please try this on Firefox and then on Chroome and see the difference. Firefox is not yet there, sadly.

Plenty fast for me in Nightly.

It's also disappointing to see that nobody in this big thread arguing about JS performance has pointed out that this demo is not JavaScript-bound at all. Profiling shows 80%-90% of the time is spent in texture upload and/or CoreGraphics. JS barely even shows up in the profile.

Post reply on HN