Why Javascript engines are so slow?

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

Nay Lin Tun
Posts: 708
Joined: Mon Jan 16, 2012 6:34 am

Why Javascript engines are so slow?

Post by Nay Lin Tun »

On chess.com and lichess.com, the javascript stockfish analysis is extremely slower than normal UCI interface. In my computer, with UCI, I get around 10Mns (depth around 25-27 instantly) but with javascript I get only about 5Mns (depth around 20). While we can easily use playstation emulator or android emulator in Window program, why not between those two languages/instructions.
User avatar
hgm
Posts: 27809
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Why Javascript engines are so slow?

Post by hgm »

That is extremely fast, for JavaScript, isn't it? JavaScript is an intrinsically slow language, because the variables are untyped, and ca hold objects of any size. So that at run time you would have to keep track of what is in the variable, and how big it is. It must have a pretty smart compiler to be only twice slower as C.

Anything better than 5% of the speed of the equivalent C program should be considered a small miracle.
mar
Posts: 2559
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Why Javascript engines are so slow?

Post by mar »

Nay Lin Tun wrote:On chess.com and lichess.com, the javascript stockfish analysis is extremely slower than normal UCI interface. In my computer, with UCI, I get around 10Mns (depth around 25-27 instantly) but with javascript I get only about 5Mns (depth around 20). While we can easily use playstation emulator or android emulator in Window program, why not between those two languages/instructions.
Only 2x slower compared to native C++? They probably used emscripten, webassembly should do even better in the near future.

Do you understand that Javascript is a dynamic language and those are extremely difficult to JIT?
That's also why modern Javascript VMs are extremely complex.
Many years ago javascript was interpreted, imagine how that would run.
User avatar
gbtami
Posts: 389
Joined: Wed Sep 26, 2012 1:29 pm
Location: Hungary

Re: Why Javascript engines are so slow?

Post by gbtami »

mar wrote:
Nay Lin Tun wrote:On chess.com and lichess.com, the javascript stockfish analysis is extremely slower than normal UCI interface. In my computer, with UCI, I get around 10Mns (depth around 25-27 instantly) but with javascript I get only about 5Mns (depth around 20). While we can easily use playstation emulator or android emulator in Window program, why not between those two languages/instructions.
Only 2x slower compared to native C++? They probably used emscripten, webassembly should do even better in the near future.

Do you understand that Javascript is a dynamic language and those are extremely difficult to JIT?
That's also why modern Javascript VMs are extremely complex.
Many years ago javascript was interpreted, imagine how that would run.
As Niklas Fiekas said they use WebAssembly:
http://talkchess.com/forum/viewtopic.php?t=63404
User avatar
gbtami
Posts: 389
Joined: Wed Sep 26, 2012 1:29 pm
Location: Hungary

Re: Why Javascript engines are so slow?

Post by gbtami »

Nay Lin Tun wrote:On chess.com and lichess.com, the javascript stockfish analysis is extremely slower than normal UCI interface. In my computer, with UCI, I get around 10Mns (depth around 25-27 instantly) but with javascript I get only about 5Mns (depth around 20). While we can easily use playstation emulator or android emulator in Window program, why not between those two languages/instructions.
Do you see this slowness with WASM on on lichess.org?
https://en.lichess.org/blog/WPSD5icAAKM ... on-lichess
User avatar
Nordlandia
Posts: 2821
Joined: Fri Sep 25, 2015 9:38 pm
Location: Sortland, Norway

Re: Why Javascript engines are so slow?

Post by Nordlandia »

Static evaluation can be found on this site ->

https://hxim.github.io/Stockfish-Evaluation-Guide/
User avatar
Guenther
Posts: 4610
Joined: Wed Oct 01, 2008 6:33 am
Location: Regensburg, Germany
Full name: Guenther Simon

Re: Why Javascript engines are so slow?

Post by Guenther »

Nordlandia wrote:Static evaluation can be found on this site ->

https://hxim.github.io/Stockfish-Evaluation-Guide/
Is this a random 'computerchess bot' post?
It has absolutely nothing to do with this thread...
https://rwbc-chess.de

trollwatch:
Talkchess nowadays is a joke - it is full of trolls/idiots/people stuck in the pleistocene > 80% of the posts fall into this category...
styx
Posts: 338
Joined: Tue Mar 13, 2012 9:59 pm
Location: Germany

Re: Why Javascript engines are so slow?

Post by styx »

The lichess.org local chess engine is no javascript. It's PNACL for Chrome and WASM for Firefox >=v52.0

It allows to run code written in several languages (C++ for Stockfish) in your browser at near native speed.

But as you experienced, it's still not 100% as fast as your native engine in your favorite GUI. But still - much faster than Javascript
tmokonen
Posts: 1296
Joined: Sun Mar 12, 2006 6:46 pm
Location: Kelowna
Full name: Tony Mokonen

Re: Why Javascript engines are so slow?

Post by tmokonen »

Guenther wrote:
Nordlandia wrote:Static evaluation can be found on this site ->

https://hxim.github.io/Stockfish-Evaluation-Guide/
Is this a random 'computerchess bot' post?
It has absolutely nothing to do with this thread...
It was obviously meant to be posted in a different thread. People do click the wrong button sometimes. I am not sure why you would be so upset about it.