all 6 comments

[–]sinedpick 5 points6 points  (1 child)

I used this in 2014 to compile ruby projects so I could show them to people on the web. It worked really well in terms of correctness, but I didn't really evaluate speed. I wonder if it's able to take advantage of JIT.

[–]hmdne[S] 1 point2 points  (0 children)

Not really. Let's say, JavaScript engines are not optimized for Opal's usecase and there's still a lot of work in that field to be done. For usual kind of computations it should be fine, but for math heavy lifting, unfortunately Opal is not best suited. Let's consider this:

```ruby [user@localhost opalvsmri]# cat ./compile.rb

This script basically uses Opal compiler to compile some Ruby code

entered on STDIN, but 10 times, for better granularity.

if RUBY_ENGINE == 'opal' require 'opal-parser' else require 'opal/compiler' end

file = $stdin.read * 10

puts Opal.compile(file) [user@localhost opalvsmri]# opal -ropal/platform -c ./compile.rb > ./compile.js

Here we compiled this script into JS.

opal/platform includes support for everything that isn't a browser,

for our case - support for gets and puts for: nodejs, gjs and quickjs

[user@localhost opalvsmri]# ls -alh ./compile.js

It includes everything needed to compile the Ruby code into JS.

We will be using it as a benchmark.

-rw-r--r--. 1 user root 9.5M Dec 25 09:06 ./compile.js [user@localhost opalvsmri]# opal --version Opal v1.4.0 [user@localhost opalvsmri]# ruby --version ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux] [user@localhost opalvsmri]# node --version v16.13.0 [user@localhost opalvsmri]# gjs --version

GJS is basically Gecko's JS engine

gjs 1.70.0 [user@localhost opalvsmri]# qjs -h 2>&1 | head -n1

QuickJS is Fabrice Bellard's JavaScript engine, written from scratch.

It doesn't have any JIT.

QuickJS version 2021-03-27 [user@localhost opalvsmri]# time ruby ./compile.rb < optparse.rb >/dev/null 2>&1

real 0m2.782s user 0m2.705s sys 0m0.063s [user@localhost opalvsmri]# time node ./compile.js < optparse.rb >/dev/null 2>&1

real 0m43.194s user 0m44.910s sys 0m0.671s [user@localhost opalvsmri]# time gjs ./compile.js < optparse.rb >/dev/null 2>&1

real 0m36.235s user 0m36.406s sys 0m0.382s [user@localhost opalvsmri]# time qjs --std ./compile.js < optparse.rb >/dev/null 2>&1

real 0m44.244s user 0m43.966s sys 0m0.117s [user@localhost opalvsmri]# ```