munchor's blog

By munchor, 10 years ago, In English

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

  • Vote: I like it
  • +15
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone please tell me if there is any downloadable version of d8. If there is, from where can I download it?

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    1. Download the binaries. (Given by bula)
    2. Extract the zip file.
    3. Copy the "v8-3.32.0" folder somewhere "SECURE" (Meaning anywhere you are fully aware of. I kept it in "C:")
    4. Add the location address to your Environment Variables -> System Variables -> Path. (In my case I added "C:\v8-3.32.0")
    5. Open a Command prompt and type d8. (similar to typing node)
    6. If all went accordingly you should see "V8 version 3.23.0 [console: dumb]".
    7. Use regular ctrl+c to exit the d8.
    8. Now instead of using "node test.js" to run the js file use "d8 test.js" and it should work.

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

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      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?