3

I agree that this might look like a question that Google has the answer to, but I could not find the answer. C++ is a language that many chess engines are built on, like Stockfish and Leela Chess Zero, but isn't Node.js more ideal because of its speed and ability compared to other languages like Python to be used as backend code and efficiency in machine learning using TensorFlow and/or Brain.js? Why are there no chess engines in Node.js then?

14
  • 55
    Node is not a language.
    – Džuris
    Dec 8 '21 at 7:47
  • 64
    What gives you the impression that Node is fast and efficient for machine learning?? Node is used for a lot of things, but ML (and more generally applications that need to be efficient) are not among them.
    – xLeitix
    Dec 8 '21 at 11:26
  • 22
    In addition to what @xLeitix said - Python, which is actually commonly used in machine learning, is infamous for being rather slow. However, this turns out not to matter, since actual heavy-lifting code is written in C/C++ or their GPU-utilizing versions, with Python used just to call those functions. In chess engine you'll probably need to do at least some heavy-lifting yourself. Dec 8 '21 at 12:59
  • 26
    Node.js is literally the total exact opposite of what you'd look for to write a chess engine.
    – Boann
    Dec 9 '21 at 1:59
  • 19
    I don't want to be un-nice to a new contributor, but isn't this a bad question? It's not really about chess. And to the extent that it is about programming, it only exists because of some pretty basic misunderstandings.
    – stannius
    Dec 9 '21 at 14:40
9

This is more of a question for Stack Overflow if you ask me... But since I've come here from there and I'm a software engineer and avid chess player, I'll try to explain.


This basically boils down to two factors, history and speed of execution. I'll handle them separately.

History

Javascript (the language Node uses) is a relatively young language. It was developed in 1996. Node, is even younger, being released in 2009. In comparison, C came out in 1972 and C++ in 1985.

The first major breakthrough in chess engines occurred when Deep Blue (written in C++) became the first chess engine to beat a grandmaster in 1997. At that point, it had been in active development for a few years and Javascript (then JScript) was a fledgling language that could do just the most basic web-based things.

As a result of this, it seems like a natural progression that future chess engines would build on the existing work and continue with C based languages. Not to mention the fact that the number of software engineers that could program in C/++ also vastly exceeded the number of JS engineers until well into the new millenium.


Speed

Chess is a hard problem computationally. It has a game-tree complexity score of 123 (exactly what this means is beyond the scope of this answer, but Noughts and Crosses has a score of 5, for comparison). As such, unless you want to be hanging around for a really long time while the computer works out its next move, your engine needs to run fast.

Javascript (and by extension Node) is an interpreted language. That means at runtime, it needs to be parsed and compiled before it can be executed. It also means that there's no opportunity for compile time optimisations to take place.

In direct juxtaposition to this, C based languages are precompiled. This means that they can execute directly at runtime with no intermediary steps and little overhead. There is also a plethora of compile-time optimisations that take place.

The end result of this is that for any given program, C/++ will perform approximately an order of magnitude faster on average. Admittedly, this has improved in the last few years somewhat, but they are still incomparable in terms of raw speed.

It must be noted, however, that speed and efficiency were far more important in previous decades. With today's computing power, even the most inefficient chess engine will run in a reasonable manner (unless it's just a dumb, extensive brute force - that will just never finish). Hence, why there are now Javascript based chess engines.


Conclusion

When you consider the history of the two languages along with the amount of computational power (and therefore the importance of efficiency) required to run a chess engine, it becomes clear why most of them are written in C based languages.

I will also add some personal opinion. C and C++, as strongly typed, procedural languages are simply far more suited to writing code for something like a chess engine. Sure, it can be done in Javascript but that was designed primarily for sending requests to a server and displaying results on a website, it's not designed for complex algorithms and the structures it uses show that.

5
  • I had to register just to chime in. :-)) "Javascript (and by extension Node) is an interpreted language.", you say. There's no such thing as an "interpreted language". Compiler or interpreter is just two different approaches to how one chooses to execute programs written in a particular language in a particular case but there's no decision inherent in any language to dictate one over the other. JavaScript can be compiled and C/C++ can be interpreted all right.
    – Gábor
    Dec 10 '21 at 22:52
  • @Gábor: Language such as JavaScript with an eval feature can't be fully ahead-of-time compiled (e.g. you at least need to include a parser and interpreter for evaled strings, and global vars need to be accessible by name to that code. And redefinition of a function by evaled code needs to be supported). It's also harder to AoT compile languages without a strong type system, so the same function could run on different combinations of object types. (potential for combinatorial explosion) Dec 11 '21 at 0:12
  • For some workloads, a good JIT can do pretty well vs. ahead-of-time compilation, and modern V8 engines are pretty good. If JS was able to efficiently express 64-bit integer math the way C uint64_t can, V8 could probably compile some functions and loops to decent native machine code. But being a JIT, it's never going to be willing to spend as much time optimizing as GCC or clang -O3, so it won't find as many optimizations. Even if all else was equal, like if JS had numbers that were exactly 64-bit, not double-precision floating point or BigInteger, not to mention BMI2 / AVX2 intrinsics. Dec 11 '21 at 0:22
  • There could be special language features and special domain languages that make one or the other approach harder than usual, yes. Still, this doesn't make the base statement invalid, general purpose programming languages have no speed, implementations have speed. And the question isn't new, either: 30 years ago, online forums of the time were already full of the same question, whether C is faster than Pascal. Nothing changes. :-)
    – Gábor
    Dec 11 '21 at 0:29
  • @Gábor - Sure, you're technically correct. That's in the most pedantic form of the term possible though. In general, languages are designed to either be interpreted or compiled, and that is how they're used, very, very few languages are routinely used in both fashions. In almost every use case, JS is interpreted, and in almost every use case C based languages are compiled. There's no point pretending that's not the case. Sure, there are exceptions, but they really are that. JIT compilation is essentially just very efficient interpretation if you ask me. Dec 14 '21 at 11:42
38

Here is a link comparing C++ to Node.js from this question on Stack Overflow.

To answer your question: C++, while more difficult to write, is way faster than basically anything else.

1
  • 4
    Comments are not for extended discussion; this conversation has been moved to chat. Yes, [language X] > [language Y] is a very important discussion but ... this is Chess Stack Exchange.
    – Glorfindel
    Dec 9 '21 at 18:19
34

Node.js isn't a language; it's a framework on top of JavaScript. It's also not fast, certainly not compared to compiled languages like C++ or Java.

Nor is JavaScript easier to use than compiled languages. If anything, it's far worse, especially for large complex applications. Of course, for a lot of people who never learn anything but JavaScript, they think JavaScript is easier to use simply because it's all they know. I for example think Java is easier to use than Python because I've been using Java for 25 years and Python only very occasionally. That doesn't mean Python is harder to use or learn; it only means I've never taken the time because I had no real need to learn it.

16
  • 1
    Java is not any more "compiled language" than JS. Both (in 99% of use cases) run in the VM.
    – Dan M.
    Dec 8 '21 at 15:38
  • 17
    @DanM. That really depends on what you mean by ‘compiled’. The usual meaning most people have these days is that a ‘compiled’ language needs to be processed into a usable form ahead of time, which would make Java a compiled language (you cannot run Java source code directly, you must compile it to either JVM bytecode or (if you’re crazy) native machine code first), but not JS (because it gets translated at runtime from the source code). Dec 8 '21 at 20:07
  • 9
    In my experience, when people speak of "compiled languages," they are referring to strongly typed languages which have their type-system behaviors compiled out during that step. Doing so gives one a lot of major performance advantages. That being said, the term "compiled language" is now a rather soft term. It used to be cut and dry, but just in time compiling made everything fuzzy.
    – Cort Ammon
    Dec 9 '21 at 0:44
  • 1
    @CortAmmon I think you've meant statically typed vs. dynamically typed. Otherwise, I'd agree that this is a more useful distinction to make, noting that nowadays you can find examples of both with the extra compilations step or not, compiled to bytecode/to run in VM or to asm/native.
    – Dan M.
    Dec 9 '21 at 8:15
  • 7
    @CortAmmon nope. There are dynamically typed languages that are compiled and statically typed languages that are interpreted. Java is statically typed and compiled, Basic (e.g.) is statically typed and interpreted (though there are compiled Basic dialects as well).
    – jwenting
    Dec 9 '21 at 8:21
27

To add onto the comments: Node.js and Python are higher-level languages/frameworks, used as "glue languages" for machine learning frameworks that just serve as human-friendly wrappers to coordinate lower level machine learning libraries that do the serious number-crunching, usually in C++ (TensorFlow and PyTorch), with other specialized GPU code in CUDA, numerical libraries in Fortran and even small bits of assembly (BLAS and LAPACK).

Using JavaScript for a frontend, like a website, or GUI is reasonable since that is not computationally demanding and JavaScript is well-suited to making user-facing frontends.

11

Node.js is just a JavaScript (JS) runtime environment that is basically just Chrome's V8 JS engine, with the APIs (set of standardized commands for interacting with something) only needed for a browser environment (e.g., open a new tab) removed and a new set of APIs needed for a console environment (e.g., changing file permissions) added. However, it's still JS underneath and is not as performant as a compiled language like C++.

It was created to allow a server's back end to use the same language as the scripts in front-end web-page that it's serving, enabling easy code reuse (very commonly validation logic for something that's usually checked on the front-end, but also on the back-end, because you can't trust the user) and only requiring a product's team to only need one language skill-set.

It's become popular outside of these bounds, simply because a lot more people have JS skills and the JS ecosystem is much bigger, so now it's used in applications where those intangible benefits outweigh its performance downsides, but chess engines are not one of those applications.

As to machine learning (ML), you have it confused with Python, another language, where ease of use and ecosystem size outweigh performance drawbacks, but Node.js has never been a major player in ML.

2
  • It is also worth noting that Nodejs itself is performant because it is written in C++ github.com/nodejs/node/tree/master/src Dec 10 '21 at 0:05
  • 2
    @erickigathi no, what's written in c++ is the shell and the system APIs, which are both irrelevant to a chess engine, the actual execution of the code is handled by the same V8 as in Chrome. Of course, you could say that V8 is itself written in C++, but it's still a layer of interpretation between the coffee and the hardware.
    – Eugene
    Dec 10 '21 at 4:36
2

There are chess engines that run on the V8 JavaScript engine used in Node.JS; pretty much all online chess games use those to offload the computation (relatively nothing today) to the client, e.g. https://www.chess.com/play/computer

Here's a standalone chess engine in JS.

Chess is popular enough to be a hobby challenge: Here are several high-level-language chess AIs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.