Блог пользователя munchor

Автор munchor, 11 лет назад, По-английски

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

Полный текст и комментарии »

  • Проголосовать: нравится
  • +15
  • Проголосовать: не нравится