Answers.Body: <p>Here is <a href="https://web.archive.org/web/20180324192118/http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=node&amp;lang2=gpp" rel="noreferrer">a link</a> comparing C++ to Node.js from <a href="https://stackoverflow.com/questions/12195841/node-js-vs-c-for-mathematic">this question</a> on Stack Overflow.</p>
  <p>To answer your question: C++, while more difficult to write, is way faster than basically anything else.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T18:19:38.287Z
Answers.Text: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/132199/discussion-on-answer-by-odinchess-com-why-dont-chess-engines-use-node-js). Yes, [language X] > [language Y] is a very important discussion but ... this is *Chess* Stack Exchange.
Answers.UserDisplayName: Glorfindel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-30T16:39:19.077Z
Answers.Text: Not necessarily faster than C, or Rust.
Answers.UserDisplayName: qwr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-07T23:47:55.077Z
Answers.LastActivityDate: 2021-12-09T01:56:14.917Z
Answers.LastEditDate: 2021-12-09T01:56:14.917Z
Answers.UserDisplayName: odinchess.com
Answers.Body: <p>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.</p>
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-08T15:38:44.493Z
Answers.Text: Java is not any more "compiled language" than JS. Both (in 99% of use cases) run in the VM.
Answers.UserDisplayName: Dan M.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-08T20:07:59.543Z
Answers.Text: @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).
Answers.UserDisplayName: Austin Hemmelgarn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-08T20:11:45.640Z
Answers.Text: @Dan https://stackoverflow.com/questions/1326071/is-java-a-compiled-or-an-interpreted-programming-language for reference
Answers.UserDisplayName: David
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-08T23:35:31.633Z
Answers.Text: @David most JS app would also need to be "processed" before they could run.  And you can do 'java HelloWorld.java' just the same. It's a meaningless distinction.
  You reference just confirms this, since replace Java with JavaScript in the top answer and it'd still be a true statement with the only difference that JS bytecode is not a useful artifact but an implementation detail of the VM the code is running on.
Answers.UserDisplayName: Dan M.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T00:44:31.993Z
Answers.Text: 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.
Answers.UserDisplayName: Cort Ammon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T08:15:42.700Z
Answers.Text: @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.
Answers.UserDisplayName: Dan M.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T08:21:03.083Z
Answers.Text: @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).
Answers.UserDisplayName: jwenting
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T15:12:18.880Z
Answers.Text: @jwenting Dan M's correction is accurate.  I should have said "statically typed" rather than strongly typed.  I will note that for most people, Java falls onto the "compiled languages" category.  The existence of "javac" is probably a factor which leads people to think and talk that way.  As for BASIC, that might be a difficult one, given how wide of a class of languages BASIC is.  Do they generally remove type information in a dedicated step before execution?
Answers.UserDisplayName: Cort Ammon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T19:14:04.060Z
Answers.Text: @CortAmmon If someone thinks that compiled means the same thing as statically-typed then they are just straight up wrong, and the term compiled isn't actually ambiguous. If the language needs to run through a compiler (whether just-in-time or not) then it's a compiled language, otherwise it isn't
Answers.UserDisplayName: Kevin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T21:45:43.907Z
Answers.Text: @Kevin What's a compiler?  I agree, that if compiler is well defined, compiled is well defined as well (and vice versa).  As a test example, what about a script which goes through a pre-processing step to replace variable names with local-variable-indexes to save runtime.  Was that a compile step?  Is python compiling to bytecode a compiling step?  On the far other extreme, its well recognized that modern CPUs don't actually execute x86 machine code.  They interpret it as microcode first.  Does that mean all programs are interpreted and not compiled?
Answers.UserDisplayName: Cort Ammon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-09T22:33:20.630Z
Answers.Text: @Kevin Python has a few implementations; CPython is compiled to bytecode, PyPy has a JIT compiler, IronPython compiles to CLR, Jython compiles to JVM. Is Python a compiled or interpreted language?
Answers.UserDisplayName: user23916
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-10T04:39:34.103Z
Answers.Text: @user23916 you're wrong. CPython is called that because the interpreter is written in C, Jython because its interpreter is written in Java, etc. etc. They're not compiled languages. Maybe there's some JIT compiling going on during interpreting cycles but that's still not a compiled language.
Answers.UserDisplayName: jwenting
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-10T09:17:44.460Z
Answers.Text: @jwenting Any CPython programmer worth their salt knows what Python [bytecode](https://docs.python.org/3/glossary.html#term-bytecode) is, how to use Python's bytecode disassembler [`dis`](https://docs.python.org/3/library/dis.html) and what the [`__pycache__` directory and accompanying `.pyc` files](https://www.python.org/dev/peps/pep-3147/) are.
Answers.UserDisplayName: user23916
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-11T00:17:43.713Z
Answers.Text: @user23916: Yes, and CPython *interprets* that bytecode.  It never JIT-compiles it to native machine code, and there is always large interpreter overhead for each Python statement (or bytecode instruction).  It saves some parsing time to load cached bytecode, but it's not at all like an optimizing compiler.  (At least that's my understanding of CPython.)
Answers.UserDisplayName: Peter Cordes
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-11T06:06:58.070Z
Answers.Text: Thanks to this discussion I just knew that Java 11 introduces executing a single-file source directly without compiling.
Answers.UserDisplayName: justhalf
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-16T23:14:44.000Z
Answers.Text: @PeterCordes It interprets the bytecode the same way Java does - in a VM. "It never JIT-compiles it to native machine code", cool never said it does. However you can JIT compile to machine code in native Python - there's a talk on that. This is the first time I've seen you miss the point. Yeah CPython is "interpreted" but has a compile step so Kevin's point is nonsense, "the term compiled isn't actually ambiguous. If the language needs to run through a compiler (whether just-in-time or not) then it's a compiled language".
Answers.UserDisplayName: user23916
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-08T11:10:42.093Z
Answers.LastActivityDate: 2021-12-09T10:51:00.793Z
Answers.LastEditDate: 2021-12-09T10:51:00.793Z
Answers.UserDisplayName: jwenting
Answers.Body: <p>To add onto the comments: Node.js and Python are higher-level languages/frameworks, used as &quot;glue languages&quot; 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 <a href="https://en.wikipedia.org/wiki/PyTorch" rel="nofollow noreferrer">PyTorch</a>), with other specialized GPU code in <a href="https://en.wikipedia.org/wiki/CUDA" rel="nofollow noreferrer">CUDA</a>, numerical libraries in <a href="https://en.wikipedia.org/wiki/Fortran" rel="nofollow noreferrer">Fortran</a> and even small bits of assembly (<a href="https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms" rel="nofollow noreferrer">BLAS</a> and <a href="https://en.wikipedia.org/wiki/LAPACK" rel="nofollow noreferrer">LAPACK</a>).</p>
  <p>Using <a href="https://en.wikipedia.org/wiki/JavaScript" rel="nofollow noreferrer">JavaScript</a> 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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-08T19:54:49.070Z
Answers.LastActivityDate: 2021-12-10T15:21:05.307Z
Answers.LastEditDate: 2021-12-10T15:21:05.307Z
Answers.UserDisplayName: qwr
Answers.Body: <p>Node.js is just a JavaScript (JS) runtime environment that is basically just Chrome's <a href="https://en.wikipedia.org/wiki/V8_%28JavaScript_engine%29" rel="noreferrer">V8</a> 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++.</p>
  <p>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.</p>
  <p>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.</p>
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-10T00:05:45.840Z
Answers.Text: It is also worth noting that Nodejs itself is performant because it is written in C++ https://github.com/nodejs/node/tree/master/src
Answers.UserDisplayName: Eric Kigathi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-10T04:36:34.003Z
Answers.Text: @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.
Answers.UserDisplayName: Eugene
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-08T19:58:40.897Z
Answers.LastActivityDate: 2021-12-09T05:14:41.837Z
Answers.LastEditDate: 2021-12-09T05:14:41.837Z
Answers.UserDisplayName: Eugene
Answers.Body: <p>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. <a href="https://www.chess.com/play/computer" rel="nofollow noreferrer">https://www.chess.com/play/computer</a></p>
  <p><a href="https://www.npmjs.com/package/js-chess-engine" rel="nofollow noreferrer">Here</a>'s a standalone chess engine in JS.</p>
  <p>Chess is popular enough to be a hobby challenge: <a href="https://rosettacode.org/wiki/Chess_player" rel="nofollow noreferrer">Here</a> are several high-level-language chess AIs.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-10T09:49:46.793Z
Answers.LastActivityDate: 2021-12-10T09:49:46.793Z
Answers.UserDisplayName: Cees Timmerman
Answers.Body: <p>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.</p>
  <hr />
  <p>This basically boils down to two factors, history and speed of execution. I'll handle them separately.</p>
  <p><strong>History</strong></p>
  <p>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.</p>
  <p>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.</p>
  <p>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.</p>
  <hr />
  <p><strong>Speed</strong></p>
  <p>Chess is a <em>hard</em> 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.</p>
  <p>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.</p>
  <p>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.</p>
  <p>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.</p>
  <p>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.</p>
  <hr />
  <p><strong>Conclusion</strong></p>
  <p>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.</p>
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-10T22:52:21.447Z
Answers.Text: 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.
Answers.UserDisplayName: Gábor
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-11T00:12:48.787Z
Answers.Text: @Gábor: Language such as JavaScript with [an `eval` feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) 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)
Answers.UserDisplayName: Peter Cordes
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-11T00:22:27.397Z
Answers.Text: 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.
Answers.UserDisplayName: Peter Cordes
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-11T00:29:39.610Z
Answers.Text: 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. :-)
Answers.UserDisplayName: Gábor
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-14T11:42:45.687Z
Answers.Text: @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.
Answers.UserDisplayName: ScottishTapWater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-10T11:33:53.833Z
Answers.LastActivityDate: 2021-12-10T11:33:53.833Z
Answers.UserDisplayName: ScottishTapWater
Body: <p>I agree that this might look like a question that Google has the answer to, but I could not find the answer. <a href="https://en.wikipedia.org/wiki/C%2B%2B" rel="nofollow noreferrer">C++</a> is a language that many chess engines are built on, like <a href="https://en.wikipedia.org/wiki/Stockfish_%28chess%29" rel="nofollow noreferrer">Stockfish</a> and <a href="https://en.wikipedia.org/wiki/Leela_Chess_Zero" rel="nofollow noreferrer">Leela Chess Zero</a>, but isn't Node.js more ideal because of its speed and ability compared to other languages like <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29" rel="nofollow noreferrer">Python</a> to be used as backend code and efficiency in <a href="https://en.wikipedia.org/wiki/Machine_learning" rel="nofollow noreferrer">machine learning</a> using <a href="https://en.wikipedia.org/wiki/TensorFlow" rel="nofollow noreferrer">TensorFlow</a> and/or Brain.js? Why are there no chess engines in Node.js then?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-08T07:47:31.870Z
Comments.Text: Node is not a language.
Comments.UserDisplayName: Džuris
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-08T11:26:01.857Z
Comments.Text: 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.
Comments.UserDisplayName: xLeitix
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-08T12:59:06.740Z
Comments.Text: 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.
Comments.UserDisplayName: val - disappointed in SE
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-08T19:04:11.160Z
Comments.Text: You can easily combine the two. Write your computationally heavy code in C/C++ and compile it to webassembly modules that you can use from nodejs and you get the speed of C++ with the comfort of a (potentially) more comfortable language for the rest :)
Comments.UserDisplayName: Yanick Salzmann
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T01:59:30.940Z
Comments.Text: Node.js is literally the total exact opposite of what you'd look for to write a chess engine.
Comments.UserDisplayName: Boann
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T07:06:53.043Z
Comments.Text: Can you even use 64-bit integers in Javascript?  JS numbers are `double` precision floating point, and some tricks like `x | 0` can get it to optimize them into integers, but I thought usually 32-bit integers.  (Most chess engines heavily use 64-bit integers as bitboards https://www.chessprogramming.org/Bitboards because there are 64 squares on a chess board.)
Comments.UserDisplayName: Peter Cordes
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T14:25:51.037Z
Comments.Text: @PeterCordes Nodejs does use 64 bits here is more info https://stackoverflow.com/questions/9643626/does-javascript-support-64-bit-integers and here is a library that makes it easier to use bitmaps https://www.npmjs.com/package/node-bitmap
Comments.UserDisplayName: Varun W.
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T14:40:37.577Z
Comments.Text: 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.
Comments.UserDisplayName: stannius
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T14:51:21.643Z
Comments.Text: @VarunW.: The SO Q&A you linked says JS *doesn't* natively do 64-bit ints, with some mention of native support for arbitrary-precision BigInt.  Unless that has some special-casing for exactly one limb that lets the JIT compiler keep the value in a register like a Java JVM JIT would for a 64-bit int type, it's going to be less efficient than C++ `uint64_t`.  (Of course still a lot better than emulation in terms of `double` or 32-bit integers like [this linked package](https://github.com/broofa/node-int64), but that's not a high bar.)
Comments.UserDisplayName: Peter Cordes
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T16:43:31.263Z
Comments.Text: @PeterCordes: Even if you could use 64 bits ints, a lot of modern engines use AVX extensively to operate on bitboards. AVX512BW even allows you to treat the ZMM registers as 64 bytes, which conveniently is enough to identify each piece. And of course it's much easier to use AVX intrinsics from C++
Comments.UserDisplayName: MSalters
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T17:11:43.347Z
Comments.Text: @VarunW. _"Nodejs does use 64 bits here is more info"_ - Read the question you linked more carefully; it is made clear that javascript does not support native 64-bit integers. (A 64-bit float is not a 64-bit integer; and BigInt is not native machine words). Also, again, node.js is not a language; javascript is.
Comments.UserDisplayName: marcelm
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-09T21:31:45.563Z
Comments.Text: (A somewhat credible reference for Brain.js is [this freeCodeCamp  article](https://www.freecodecamp.org/news/want-to-learn-neural-networks-heres-a-free-brain-js-course-merry-christmas-ea801f378041/).)
Comments.UserDisplayName: Peter Mortensen
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-10T19:36:29.710Z
Comments.Text: In case you aren't familair with why there's so much focus on 64-bit integers, it's because one of the major formats used to represent the boards are "bitboards," which use the bits of a 64-bit integer to represent the pieces.  It has nothing to do with bitmatps.  We use bit boards because they are *fast*.  We can do bitwise operations on them to calculate many useful things, and these operations can be done in 1-4 cycles.  Use of an arbitrary-precision library like BigNumber or Int64 would be utterly disasterous to efficiency.
Comments.UserDisplayName: Cort Ammon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-11T03:56:38.627Z
Comments.Text: @Džuris Why isn't it? I mean, it's closer to an implementation of a language, but it _does_ add a lot of stuff on top, and in basically any context it makes sense to consider it a language on its own. "Node is not a language" just sounds like pointless pedantry.
Comments.UserDisplayName: rydwolf
Tags: <engines><software><programming><chess-ai>
Title: Why don’t chess engines use Node.js?
ThreadText: # Why don’t chess engines use Node.js?
  
  (Asked by: username_0 on 2021-12-07)
  
  I agree that this might look like a question that Google has the answer to, but I could not find the answer. [C++](https://en.wikipedia.org/wiki/C%2B%2B) is a language that many chess engines are built on, like [Stockfish](https://en.wikipedia.org/wiki/Stockfish_%28chess%29) and [Leela Chess Zero](https://en.wikipedia.org/wiki/Leela_Chess_Zero), but isn't Node.js more ideal because of its speed and ability compared to other languages like [Python](https://en.wikipedia.org/wiki/Python_%28programming_language%29) to be used as backend code and efficiency in [machine learning](https://en.wikipedia.org/wiki/Machine_learning) using [TensorFlow](https://en.wikipedia.org/wiki/TensorFlow) and/or Brain.js? Why are there no chess engines in Node.js then?
  
  **Tags:** engines, software, programming, chess-ai
  
  > **Comments:**
  > **username_1**: Node is not a language.  
  > **username_2**: 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.  
  > **username_3**: In addition to what @username_2 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.  
  > **username_4**: You can easily combine the two. Write your computationally heavy code in C/C++ and compile it to webassembly modules that you can use from nodejs and you get the speed of C++ with the comfort of a (potentially) more comfortable language for the rest :)  
  > **username_5**: Node.js is literally the total exact opposite of what you'd look for to write a chess engine.  
  > **username_6**: Can you even use 64-bit integers in Javascript?  JS numbers are `double` precision floating point, and some tricks like `x | 0` can get it to optimize them into integers, but I thought usually 32-bit integers.  (Most chess engines heavily use 64-bit integers as bitboards https://www.chessprogramming.org/Bitboards because there are 64 squares on a chess board.)  
  > **username_0**: @username_6 Nodejs does use 64 bits here is more info https://stackoverflow.com/questions/9643626/does-javascript-support-64-bit-integers and here is a library that makes it easier to use bitmaps https://www.npmjs.com/package/node-bitmap  
  > **username_7**: 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.  
  > **username_6**: @VarunW.: The SO Q&A you linked says JS *doesn't* natively do 64-bit ints, with some mention of native support for arbitrary-precision BigInt.  Unless that has some special-casing for exactly one limb that lets the JIT compiler keep the value in a register like a Java JVM JIT would for a 64-bit int type, it's going to be less efficient than C++ `uint64_t`.  (Of course still a lot better than emulation in terms of `double` or 32-bit integers like [this linked package](https://github.com/broofa/node-int64), but that's not a high bar.)  
  > **username_8**: @username_6: Even if you could use 64 bits ints, a lot of modern engines use AVX extensively to operate on bitboards. AVX512BW even allows you to treat the ZMM registers as 64 bytes, which conveniently is enough to identify each piece. And of course it's much easier to use AVX intrinsics from C++  
  > **username_9**: @VarunW. _"Nodejs does use 64 bits here is more info"_ - Read the question you linked more carefully; it is made clear that javascript does not support native 64-bit integers. (A 64-bit float is not a 64-bit integer; and BigInt is not native machine words). Also, again, node.js is not a language; javascript is.  
  > **username_10**: (A somewhat credible reference for Brain.js is [this freeCodeCamp  article](https://www.freecodecamp.org/news/want-to-learn-neural-networks-heres-a-free-brain-js-course-merry-christmas-ea801f378041/).)  
  > **username_11**: In case you aren't familair with why there's so much focus on 64-bit integers, it's because one of the major formats used to represent the boards are "bitboards," which use the bits of a 64-bit integer to represent the pieces.  It has nothing to do with bitmatps.  We use bit boards because they are *fast*.  We can do bitwise operations on them to calculate many useful things, and these operations can be done in 1-4 cycles.  Use of an arbitrary-precision library like BigNumber or Int64 would be utterly disasterous to efficiency.  
  > **username_12**: @username_1 Why isn't it? I mean, it's closer to an implementation of a language, but it _does_ add a lot of stuff on top, and in basically any context it makes sense to consider it a language on its own. "Node is not a language" just sounds like pointless pedantry.  
  
  ---
  
  ## 6 answers
  
  ### Answer by username_26 on 2021-12-10. Score: 13 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_27**: 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.  
  > **username_6**: @username_27: Language such as JavaScript with [an `eval` feature](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) 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)  
  > **username_6**: 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.  
  > **username_27**: 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. :-)  
  > **username_26**: @username_27 - 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.  
  
  ---
  
  ### Answer by username_13 on 2021-12-07. Score: 36
  
  Here is [a link](https://web.archive.org/web/20180324192118/http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=node&lang2=gpp) comparing C++ to Node.js from [this question](https://stackoverflow.com/questions/12195841/node-js-vs-c-for-mathematic) on Stack Overflow.
  
  To answer your question: C++, while more difficult to write, is way faster than basically anything else.
  
  > **Comments:**
  > **username_14**: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/132199/discussion-on-answer-by-username_13-why-dont-chess-engines-use-node-js). Yes, [language X] > [language Y] is a very important discussion but ... this is *Chess* Stack Exchange.  
  > **username_15**: Not necessarily faster than C, or Rust.  
  
  ---
  
  ### Answer by username_16 on 2021-12-08. Score: 35
  
  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.
  
  > **Comments:**
  > **username_17**: Java is not any more "compiled language" than JS. Both (in 99% of use cases) run in the VM.  
  > **username_18**: @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).  
  > **username_19**: @Dan https://stackoverflow.com/questions/1326071/is-java-a-compiled-or-an-interpreted-programming-language for reference  
  > **username_17**: @username_19 most JS app would also need to be "processed" before they could run.  And you can do 'java HelloWorld.java' just the same. It's a meaningless distinction. You reference just confirms this, since replace Java with JavaScript in the top answer and it'd still be a true statement with the only difference that JS bytecode is not a useful artifact but an implementation detail of the VM the code is running on.  
  > **username_11**: 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.  
  > **username_17**: @username_11 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.  
  > **username_16**: @username_11 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).  
  > **username_11**: @username_16 username_17's correction is accurate.  I should have said "statically typed" rather than strongly typed.  I will note that for most people, Java falls onto the "compiled languages" category.  The existence of "javac" is probably a factor which leads people to think and talk that way.  As for BASIC, that might be a difficult one, given how wide of a class of languages BASIC is.  Do they generally remove type information in a dedicated step before execution?  
  > **username_20**: @username_11 If someone thinks that compiled means the same thing as statically-typed then they are just straight up wrong, and the term compiled isn't actually ambiguous. If the language needs to run through a compiler (whether just-in-time or not) then it's a compiled language, otherwise it isn't  
  > **username_11**: @username_20 What's a compiler?  I agree, that if compiler is well defined, compiled is well defined as well (and vice versa).  As a test example, what about a script which goes through a pre-processing step to replace variable names with local-variable-indexes to save runtime.  Was that a compile step?  Is python compiling to bytecode a compiling step?  On the far other extreme, its well recognized that modern CPUs don't actually execute x86 machine code.  They interpret it as microcode first.  Does that mean all programs are interpreted and not compiled?  
  > **username_21**: @username_20 Python has a few implementations; CPython is compiled to bytecode, PyPy has a JIT compiler, IronPython compiles to CLR, Jython compiles to JVM. Is Python a compiled or interpreted language?  
  > **username_16**: @username_21 you're wrong. CPython is called that because the interpreter is written in C, Jython because its interpreter is written in Java, etc. etc. They're not compiled languages. Maybe there's some JIT compiling going on during interpreting cycles but that's still not a compiled language.  
  > **username_21**: @username_16 Any CPython programmer worth their salt knows what Python [bytecode](https://docs.python.org/3/glossary.html#term-bytecode) is, how to use Python's bytecode disassembler [`dis`](https://docs.python.org/3/library/dis.html) and what the [`__pycache__` directory and accompanying `.pyc` files](https://www.python.org/dev/peps/pep-3147/) are.  
  > **username_6**: @username_21: Yes, and CPython *interprets* that bytecode.  It never JIT-compiles it to native machine code, and there is always large interpreter overhead for each Python statement (or bytecode instruction).  It saves some parsing time to load cached bytecode, but it's not at all like an optimizing compiler.  (At least that's my understanding of CPython.)  
  > **username_22**: Thanks to this discussion I just knew that Java 11 introduces executing a single-file source directly without compiling.  
  > **username_21**: @username_6 It interprets the bytecode the same way Java does - in a VM. "It never JIT-compiles it to native machine code", cool never said it does. However you can JIT compile to machine code in native Python - there's a talk on that. This is the first time I've seen you miss the point. Yeah CPython is "interpreted" but has a compile step so username_20's point is nonsense, "the term compiled isn't actually ambiguous. If the language needs to run through a compiler (whether just-in-time or not) then it's a compiled language".  
  
  ---
  
  ### Answer by username_15 on 2021-12-08. Score: 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](https://en.wikipedia.org/wiki/PyTorch)), with other specialized GPU code in [CUDA](https://en.wikipedia.org/wiki/CUDA), numerical libraries in [Fortran](https://en.wikipedia.org/wiki/Fortran) and even small bits of assembly ([BLAS](https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms) and [LAPACK](https://en.wikipedia.org/wiki/LAPACK)).
  
  Using [JavaScript](https://en.wikipedia.org/wiki/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.
  
  
  ---
  
  ### Answer by username_23 on 2021-12-08. Score: 11
  
  Node.js is just a JavaScript (JS) runtime environment that is basically just Chrome's [V8](https://en.wikipedia.org/wiki/V8_%28JavaScript_engine%29) 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.
  
  > **Comments:**
  > **username_24**: It is also worth noting that Nodejs itself is performant because it is written in C++ https://github.com/nodejs/node/tree/master/src  
  > **username_23**: @username_24 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.  
  
  ---
  
  ### Answer by username_25 on 2021-12-10. Score: 3
  
  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](https://www.npmjs.com/package/js-chess-engine)'s a standalone chess engine in JS.
  
  Chess is popular enough to be a hobby challenge: [Here](https://rosettacode.org/wiki/Chess_player) are several high-level-language chess AIs.
  
  
  ---
  
  