JavaScript code size #5124
Replies: 1 comment
-
Posted at 2015-11-04 by asez73 Hi, @gfwilliams! Posted at 2015-11-04 by @allObjects Some things just have to be left alone. May be stating this information in a different way is more helpful: the IDE allows from 0 to maximum compression/minification, and the first one is taking spaces out. Any code reaching minimal maturity level and any code that has to perform is put to runtim taking any available advantage of runtime enahncement options. Why would I leave spaces in the uploaded code when not having to, but could if I wanted for what so ever reason? - Nobody prevents me to drive off with half pulled emergency breakes... any decent enging will get over it... but not my wallet after a while... enjoy [hotrod mag borrowed] (https://www.facebook.com/hotrodmag/photos/a.199145132539.141591.13601527539/10153079083752540/?type=3) attachment. Attachments: Posted at 2015-11-05 by @gfwilliams Wow, impressive! Never seen someone manage to do that before! The latest builds (so 1v82) have a few more space-saving tweaks which should help you squeeze a bit more out of it (~20%). There's still a little room for improvement, but yes - the level of memory available is such that it's entirely possible to write enough code that you'll run out of memory :( It was actually a bit of a surprise just how much more compact minified code was - however the code used is quite maths-heavy and I'm sure you could find cases where minified code wasn't as good... but it does make you wonder what something like Web Assembly would really add. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-11-04 by @gfwilliams
I got a bit fed up with people randomly linking to the performance page from Twitter and sniggering about whitespace changing execution speed.
I came up with this - thought you might like it:
Why not compile to native/bytecode?
Memory is scarce on microcontrollers, and we want the source code on the device itself so we can edit and debug it without external tools.
There isn't enough room on the microcontroller for source code and compiled code, but luckily source code is surprisingly memory-efficient.
For instance take this code that draws a Mandelbrot fractal:
It's 301 bytes long.
When compiled with SpiderMonkey, the following bytecode is created (obtained by running a debug build and
dbg(function() { .... })
:It's 270 bytes long. So you've saved 31 bytes over the original code, but now your code is totally uneditable and unreadable.
If you compiled this into native code with the Espruino Compiler, the size of the binary would be 1136 bytes.
But what if you rewrote it in C and compiled it in GCC with size optimisation turned on. That'll be efficient, right?
Nope. 290 bytes.
However, if you minified your code with the closure compiler you'd get:
It's editable, just about readable, and it's only 167 bytes - so it is smaller than bytecode and even highly optimised native code!
-Os
)So by executing from source, we use around the same amount of memory as we would if we compiled or used bytecode, while still having everything we need to edit and debug the code on-chip.
However, if you need to make things smaller, you can minify the JavaScript functions you don't need to edit, which will use less RAM than even size-optimised C code!
Beta Was this translation helpful? Give feedback.
All reactions