Multithreading with JavaScript (JSEN)

Research Paper by Lead Scientists of Honda Research Institute & Rinf.tech Contributors

Sequential

Store and Execute Sequential Notation

Multi-threading

Add virtual multi-threading capabilities to Java Script

Virtual Language

Allow creation of DSL (Domain Specific Languages) in a very simple way

Valid Alternative

To Java web workers and promises

JSEN Purpose

JSEN is a data structure that can be used for storing and manipulating executable JavaScript code. This is useful for storing and exchanging executable code. It can also be used to bring concurrent execution based on virtual threads inside JavaScript. This structure is called JSEN or JavaScript Executable Notation. This is the counterpart of JSON, JavaScript Object Notation.

JSON vs JSEN

Where JSON is a format for storing and exchanging data, JSEN is a format for storing and exchanging executable code. Both share a very similar structure as illustrated in the snippets bellow.

Example of a JSON data structure

				
					{
  "volume": 5,
  "current": {
    "band": "Iron Maiden",
	"song": "Phantom of the Opera",
	"members": [
	  {"name": "Paul Di'Anno", "role": "vocals"},
	  {"name": "Dave Murray", "role": " guitar"},
	  {"name": "Dennis Stratton", "role": "guitar"},
	  {"name": "Steve Harris", "role": "bass guitar"},
	  {"name": "Nicko McBrain", "role": "drums"},
	  {"name": "Clive Burr", "role": "drums"}
	]
  }
  "next": {
    "band": "Metallica",
	"song": "The Unforgiven II",
	"members": [
	  {"firstname": "James Hetfield", "lastname": "vocals, rhythm guitar"},
	  {"firstname": "Kirk Hammett", "lastname": "lead guitar"},
	  {"firstname": "Jason Newsted", "lastname": "bass"},
	  {"firstname": "Lars Ulrich", "lastname": "Drums"}
	]
  }
}
				
			

Example of a JSEN program

				
					[
  ()=> result = '',
  'Start finding divisors for 100 numbers',
  JSEN.for( 'i', 1, 100 ),
  [
    JSEN.get('i', (value)=> current = value),
    ()=> {
      nTest = 1;
	  result = number + ': ';
	},	
    JSEN.while( ()=> nTest <= current ), // Check divisors
    [
      JSEN.if( ()=> current % nTest == 0 ),
	  [
        ()=> result += nTest + ' ',
        ()=> ++nTest,
	  ],
    ],
	()=> console.log( result ),
  ],
]
				
			
JSEN is composed around anonymous functions, pure JSEN statements, strings, arrays, and objects. JSEN structures can be easily serialized and deserialized via the JSEN.stringify and JSEN.parse, functions provided by the basic JSEN class. The array structure of JSEN also provides a base for implementing virtual threads in JavaScript

Concurrency with JSEN

The granularity of JSEN statements allows the possibility of implementing a new form of multitasking in JavaScript. By using the JSENVM library, multiple JSEN Threads can be started. It is also easy to write custom scheduling algorithms that can run the code contained in multiple JSEN structures concurrently.

Paper and Repo

The development of JSEN was financed by Honda Research Institute GmbH. The conclusions of the work are summarized in the research paper and GitHub repository bellow.

Example of a JSEN multi threaded program

				
					const JSEN = require( ‘JSEN’ );
const JSENVM = require( ‘JSENVM’ );

let dataArray = [];

let counter = 0;
const producer = [
  JSEN.loop(),
  [
    ()=> ++counter,
	()=> dataArray.push( counter ),
	JSEN.sleep( ()=> Math.random() * 2 ),
  ],
];

let data;
const consumer = [
  JSEN.loop(),
  [
    JSEN.if( ()=> dataArray.length !== 0 ),
	[
	  ()=> data = dataArray.shift(),
	  ()=> console.log("Consumed: " + data),
	],
	JSEN.sleep( ()=> Math.random() * 2 ),
  ],
];

JSENVM.run( producer, consumer );
				
			

From JSON to JSEN through Virtual Languages

Open Journal of Web Technologies (OJWT), Vol.8, Issue 1, 2021

Want to contribute further?

Let’s talk.