Recently, the Codeforces team announced that they were adding support for problem resolution using JavaScript. I was quite glad with these news and have since started using JavaScript in every contest.
Running and testing the code locally
I use d8
, the JavaScript interpreter in Google's V8 JavaScript engine. I run different test cases using redirected input and output calling d8
on my shell. If I need to quickly test any JavaScript syntax, calling d8
by itself lets me run JavaScript commands as with any interpreter (think Python).
JavaScript and IO
d8
provides IO functions such as print()
(automatic newline), write()
and readLine()
. I prefer C's IO, but using readLine
, I can simplify code a lot using split
and map
. For example, say I want a one-liner to read a list of space-separated integers without knowing how many numbers are on the line, then I can simply run:
var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
Performance
JavaScript engines are becoming more and more efficient as companies like Mozilla and Google push the boundaries of SpiderMonkey and V8, respectively, to execute JavaScript faster. The speed of JavaScript is still not comparable to the speed of C and C++ (of course), but it is noticeably faster than most interpreted languages such as Python or Ruby and still provides us with quite a lot of syntactic sugar like these.
JavaScript is not just about the Web
A lot of people still think JavaScript is just a language browsers use, but it's a whole different beast. You can use it for all sorts of stuff and I'll definitely keep using it for Codeforces (even if just now and then).
Can anyone please tell me if there is any downloadable version of d8. If there is, from where can I download it?
Windows binaries.
can you please make it clear how to test js code locally using d8. i.e., installing d8 how to use it. I was not able to use readline() to get input from process.stdin. :(
One small thing to note is, I created a bin folder inside "C:\v8-3.32.0" and copied only the "d8.exe" file inside it. Then added "C:\v8-3.32.0\bin" in the Environment variables. I did this because there were also many other .exe files inside "C:\v8-3.32.0" that I think are not necessary to be passed as varibles. (I may be wrong. :( ) Just to be on the safe side don't do this. (Or do this if the above does not work).
The binaries are too old and doesn't support ES6. Can you provide me with a link to the latest binaries that support ES6 as well?