Rook
РусскийRUGet started
Newgo for: a loop over an array on every core

JavaScript on every core.

Rook is a language with the semantics of JavaScript, the syntax of Go and static types. The word go runs a function on another processor, channels connect the goroutines, and it all compiles to readable JavaScript.

npm install rooklang
Watch this program move by move
move 12 of 24 · main

<-results: main meets square 1, who was waiting, and takes 1.

One thread, or every core.

go for splits an array into parts, several per processor, runs each part as a goroutine on the first free core and waits for all of them. The same loop, one word longer.

8.1×faster

primes is an array of ten million numbers. Each one has to be checked.

for i, _ in primes {
    primes[i] = isPrime(i) ? 1 : 0
}

one thread0 ms

go for i, _ in primes {
    primes[i] = isPrime(i) ? 1 : 0
}

every core0 ms

Open in playground

Measured with Node on a machine with 14 cores: a rook for each core where the screen has the room.

The ideas of Go, in JavaScript.

Threads, channels and types that know about them, in a language that compiles to plain JavaScript for the browser and Node.

Goroutines on real threads

go f(x) runs a function in a pool of Web Workers or worker threads, one per processor, and gives a Promise of its result. No worker files, no messages written by hand.

const n = await go countPrimes(0, N)

Channels and select

Goroutines in any thread pass values through channels, wait on several at once and stop waiting with after(ms), as in Go.

select {case msg = <-messages:
    console.log(msg)case <-after(500):
    console.log("timeout")}
Watch it move by move

Types that know about threads

A function, a class or a DOM element cannot be copied to another thread. The compiler says so before the program runs.

main.rk:3:9: error: the goroutine runs in a worker, which gets a copy of "title", but a copy of HTMLElement would lose its methods

Components in the browser

comp, state and markup compile to direct calls of the DOM, without a virtual DOM. This counter is a component of this page:

state count = 0
<button onClick={count++}>

Components on the server

The same markup writes a server: there it is a string of HTML, a component is an async function, and the root answers a request. Experimental until 1.0.

comp Page(request Request) {
    const user = await currentUser(request)
    return <h1>Hello, {user.name}</h1>
}

serve(Page, { port: 3000 })

Tools included

A CLI that builds and runs, a Vite plugin, an extension for VS Code with errors as you type and completion, and .d.ts files for TypeScript.

rook buildrook runrooklang/viteVS Code.d.ts

Write Rook. Ship JavaScript.

Each construct becomes the JavaScript you would write by hand. What channels and goroutines need comes from a small runtime, imported by name.

app.rk
funcfunctionfib(nnumber)number{returnn<2?n:fib(n-1)+fib(n-2);}// fib runs in app.worker.js, in a pool of workersconstresults=awaitPromise.all([go$$go("fib(",[40]),go$$go("fib(",[41])]);console.log(results);

Start in a minute.

The compiler is an npm package and runs on Node.js 24.

1Install

Terminal
$ npm install rooklang

2Write

hello.rk
func greet(name string) string {
    return `Hello, ${name}!`
}

console.log(greet("Rook"))

3Run

Terminal
$ npx rook run hello.rk
Hello, Rook!

Start with the guide.

From variables to goroutines and components, with examples you can run right on the page.