JS1k demo: “Highway at Night”
js1k.com
js1k.com
// Moon
c.fillText("(",99,-99);This makes you realise how old games must have been driven by the technology.
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".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.
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.
It only works in firefox and if I remember correctly, it is no longer part of the latest ECMA 6 spec.
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.
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.
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.
Things like that are only acceptable if you're optimizing a bottleneck.
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) var exists = ~someStringOrArray.indexOf(searchTerm);
if(exists){...}
or // If exists do...
if(~someStringOrArray.indexOf(searchTerm)) > '15'
'15'
> ~~'15'
15
> '15'|0
15
> 15.99|0
15
> ~~15 * 1.3
19.5
> 15 * 1.3 | 0
19Part 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).
I wouldn't recommend anyone doing optimizations in especially JavaScript unless it's really needed. Remember that optimizations are the root of all evil!
My favorite: http://js1k.com/2014-dragons/demo/1868
As a side note: I'm in college now and looking to propose an independent study on compression. Any suggested readings or algorithms I should look into?
Think 1000.0 vs 1E3, printf("foo") vs puts("foo"), etc.
Lossy compression (which RegPack is actually an example of), typically involves lossless compression plus a preprocessing step to discard information so that the lossless compressor can do a better job.
The techniques for discarding information vary based on application. For images and audio, the most successful techniques tend to involve working in frequency space, so you'll want to read up on the FFT and related transforms.
For code compression, you have different constraints when discarding information because you have to produce a program with identical output to the original. Simple techniques involve removing comments and renaming variables to have shorter names. More advanced techniques involve more complex transformations of the AST, including language-specific tricks like wrapping javascript in a "with(Math){}" block.
Hope some of that helps.
Curious: Does any of your work survive?
for(T=[B=F=Z=D=i=0];i<600;i++)
The key here is that = is a right-associative expression operator which yields the right operand.
The block quotes provide containment.
T=[0],B=0,F=0,Z=0,D=0,i=0So please try this on Firefox and then on Chroome and see the difference. Firefox is not yet there, sadly.
I don't need my browser to drive full-screen 3D games & demos on my current-laptop hardware. That's not what I (or anyone else) is currently using their browser for. Maybe 5 years from now things will have changed. Maybe then truly everything will be in the browser. That's fine, but by then I will have bothered buying a newer, more powerful laptop, and the browser-engines will be even more improved and everything will probably be fine.
But right now? Performance gap between browsers being put to use they're not being used for? Watch me care less.
Honestly I'd much rather have people stop writing Chrome-only websites and going back to web-standards than having a faster JS-engine in my open-source browser, because that's not where things hurt these days.
Edit: the hatred for web standards in this thread is staggering. How about a reply saying why you don't think a fragmented web is a problem instead of silently downvoting a legitimate concern?
Chrome is open-source Chromium plus an auto-updater and Flash.
Chromium! = Chrome and more people who realize that (and vocalize it) the better.
Do you honestly not have a problem with that?
edit: also, these days browsers work together in standardization communities and implement stuff from other browsers. This was basically non-existent at IE6 time, in addition to IE6 being basically frozen and closed for years.
So yes I am shocked to hear that you don't care. And I'm a bit shocked to see you rant at a person that simply suggested Firefox's JS engine may not be as performant as Chrome's.
This is serious FUD with no evidence.
Which leaves the issue of your privacy: do you know what Google records of your data when using chrome, where it gets uploaded and stored, and how often?
I don't, and there's no way to know (due to closed source nature) so I don't trust it. That's not FUD. Those are facts.
A lot of security experts out there would be making noise if they discovered that Chrome was phoning home, or doing anything overtly malicious.
http://www.favbrowser.com/google-chrome-spyware-confirmed/
There's probably more, especially with sync enabled, like Google permanently storing your web site history, which cannot be verified from the client alone, because the server sync engine is 100% closed and hosted by Google.
Sync is pretty obvious since you clearly have no control over the backend. However, that is a non-issue for someone paranoid enough about where their data goes. Clearly you don't enable it.
I think a healthy dose of paranoia in today's world is fine. I worry a lot about what apps are sending what data where, and whether I have control over that. You, however, seem to just be making blatant claims and touting them as fact. You are part of the problem because you are spouting what amounts to misinformation, and not considering that we have tools available to us with which to verify what an application actually does and whether it is malicious or not.
I also remember that IE used to have far better canvas performance than Chrome or Firefox since IE9 was the first browser to hardware-accelerate 2D rendering.
From other comments in this thread, it sounds like things are improving in newer Firefox versions. There will always be worst-cases, but generally, I wouldn't say Chrome is really faster than Firefox.
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.
The human brain is a master at recognising and codifying geometric 3d shapes. The developer who created this is a master at coding geometric shapes and transitions in javascript as well.
Also, shouldn't the number be expressed in bits rather than bytes?
There's no need for guesswork! Just dive into the literature.
Human memory is built precisely around remembering geographic locations, human faces and images[1]. The 'have I seen this before feeling' you get is your brain trying to match the demo location to memories ( my conjecture anyway )
http://www.pouet.net/prod.php?which=61668
Making of: http://www.ylilammi.com/webgl/highway4k/Making%20of%20Highwa...
I'd recommend watching the video instead.
It's really impressive and he posted the explanation: http://birdgames.nl/2014/04/js1k-post-mortem-minecraft/
Edit: much better in 35.0a2. I wonder what they did?
Programming is a form of art, to me.
1.) people can take away a lot from the code and animation which makes it a great example of a new art form. The street scene in oarticular conjures up emotions.
2.) just because a group of the Internet does not understand or appreciate art in all its forms, doesn't mean anything at all except they are ignorant when it comes to identifying artwork.