De‐bloating Javascript

Source: github.com
24 points by Claudius 14 hours ago on lobsters | 15 comments

De-bloating Javascript

Version française

No really... The syntax seems to have been invented by someone who wanted to bet that he could push more brackets in a code than C++ and Lisp together.

newNames.forEach((name, i) => {
    allAgentContents[name] = contents[i];
    agentModes[name] = modes[i];
    if (compiled[i]) agentCompiledCache[name] = compiled[i];
    agentViewingCompiled[name] = viewing[i];
});

Who could come up with a syntax as bloated as this? You open a parenthesis, and within the parentheses you open a curly bracket, and you have to close all of them in the right order. There are cases when you have at least five or six closing parentheses thrown into the dough with closing curly brackets and a few square brackets, until the whole thing becomes indigestible.

But the worst part is that the language itself is pretty lacking when you need string or calculus functions. You then download a ton of libraries to do stuff that is usually part of any basic programming language.

Ok. I agree, it is not as bad as C or C++, where you need includes to do anything. However, the problem in the case of JS is that most of the time, you have no idea who implemented the library in the first place and who is supposed to maintain the bloody thing.

Sometimes, a page will load half of the internet to display hello in a small window.

And the internet depends on that language. You can complain all you want, you can hide it in TypeScript, but the thing is still there, like a giant that guards a bridge you need to cross. A dumb, very large giant that you have to pay your respects to every time you open a page in your browser.

There was a time when people dreamt of the browser as the ultimate OS. The one that would rule them all. The one that would make Windows or Mac OS redundant at last. It was a beautiful dream, a wonderful rêverie where your life would blossom in the colorful pages of your browser.

And they chose Javascript to embody this dream.

Web Assembly

But now, we have Web Assembly, which is akin to a virtual machine with its own machine code. This opens new frontiers to a different sort of coding, right?

Some people have tried, and many have fought against the machine. Pyodide is an impressive feat of engineering that runs Python in the browser, but it illustrates the cost of fighting JavaScript on its own terrain: you end up with two asynchronous worlds talking to each other, Python's asyncio on one side, the JS event loop on the other, and a fragile bridge in between where every await has to remember which universe it lives in.

Resources in the browser are scarce, as was the case when computer science started back in the fifties. I started on a computer in 1981 that had exactly 15772 bytes left to program. It creates a specific mindset, where every instruction, every structure must be investigated to the bit.

To be honest, the browser is not as limited in terms of memory as my first computer, but still, the way Web Assembly works creates an environment where careful implementation is required. You don't own the memory as in a regular program. You need to ask permission first, in a very constrained way.

And furthermore, you don't fight Javascript, you bargain with it.

LispE

She is rich. Rich, powerful...

LispE offers more than 450 functions in one single binary. A WASM binary that is very light. Lighter than the picture of your cat in an Instagram feed: 3.3 MB. It offers functions to handle strings, calculus, matrices, regular expressions, all packed in one single place, ripe for the picking.

And the API gives you a control you wouldn't expect for such a tiny interpreter. You can return strings, Float64Array, integers, floats, arrays of strings.

And LispE is a Lisp: the AST is alive... It's alive.

If you don't like the syntax, we propose transpilation grammars for languages indistinguishable from Python or Basic, or any other style you would dream of. Modify the grammar and you create your own language. In Greek, if you want. We already have.

LispE doesn't fight the Giant. It parlays with him, exchanges greetings, taps into its treasure trove of functions and DOM capabilities. You become his best friend, and he invites you to his table as a guest, not as the main meal.

Pay the toll

If you want to pay the toll to the Giant, LispE offers evaljs and asyncjs to execute some Javascript code from within LispE:

(setq a (evaljs "10 + 20 + 30")) ; execute some JS code

; call_llm is a user-defined JS function in the page
(asyncjs `call_llm("Implement a piece of code in Python to sort strings");` 'mycallback)

(defun mycallback(theresult) ...)

asyncjs is a Promise to the Giant to come back once his task is over.

De-bloat your world

On the other side of the bridge, a man is seated on a bench, his face ashen. "I knew," he says. "I said it in 1995. I, Wirth, said that faster computers and larger models would not solve our problems. We need to keep our code lean."

He looks at you, then at the small binary that has just crossed the bridge. Small as a hobbit, swift as the Mearas.

"Look what LispE proposes: 450 functions exposed in the browser, in a single auditable binary."

For each of these functions, you would otherwise need to load libraries such as mathjs for numerical computation, lodash for collections, voca for string manipulation, simple-statistics for statistical distributions, and so on. Hundreds of megabytes of code, each with its own author, its own bugs, its own maintenance schedule.

With LispE, you have one single maintained code that provides all this for free. Because LispE is an Open Source project, and its code is fully auditable. No surprises, no Garbage Collector to bring your code to its knees at the worst moment. It communicates with JS in a transparent way, through a simple API call that can return predefined structures:

// floats here is a Float64Array
const floats = callEvalLispEToFloats(0, `(normal_distribution 100)`);

And I can show you how LispE and Javascript can work hand in hand, right here.

If you want to integrate lispe in your Javascript applications, you'll find everything you need: Here Are The Files.

The demonstrator files are here: Here Are The Demonstrator Files