Comparison of module method declarations #537
Replies: 12 comments
-
Posted at 2016-03-07 by @gfwilliams If you're using objects I'd say If you do:
then even once initialised, Espruino will still contain The important thing to remember is that Espruino actually executes the module code at the time it is uploaded, so:
Is executed, and you end up with effectively:
So it doesn't actually matter if you manage to take few bytes off the minified module. What matters is the amount of memory that's used ( |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-07 by @gfwilliams Another example is:
So |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-08 by CanyonCasa Makes sense. When you say "actually executes the module" does it really execute or does it enterpret to prep for execution? Does the same apply to code loaded outside a module at the main script? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-08 by @gfwilliams It really does execute the code. For example:
will create a 100 element array called However functions (and what is in them) won't be executed (unless you explicitly execute them). So:
won't create an array. All that is stored is some data representing the function, and the source code inside it. It might be worth taking a look at this: http://www.espruino.com/Performance |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-08 by Wilberforce Looking at your link above lead me to here: http://www.espruino.com/Compilation This only works for the arm boards right? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-08 by @gfwilliams Yeah... Potentially someone could host their own compiler, but it could be quite difficult. You need to be able to make self-contained position independent code, which required some trial and error with compiler flagsd (at least for me). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-08 by @gfwilliams However I should point out that compiling your code does not make it smaller. In fact it usually drastically increases the size. Minified JS code is actually extremely efficient, so the only reason you'd use compiled code is for the speed benefit - and you only really get that for code that does a lot of integer work. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-08 by Wilberforce Thanks. I've been looking at the size of the FlashStore modules I've made. It looks like you can gain quite a bit by shorting variable names I.e, address -> addr and shorting up internal this.xxx vars. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-09 by CanyonCasa Thanks. I have looked at Performance page, Compilation, Inline Assembler, and other related documentation, but it took a couple more examples to sink in. When you said execute, I was confused by the exact example you showed above where the function would not be executed until actually called. This has helped a bunch. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-09 by @gfwilliams No problem. I'll see if I can stick that example into one of those pages. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-10 by @allObjects @CanyonCasa, take a look at this and this post. It may give you some understanding what is processed and what not and the major difference to other MC technologies, such as Arduino. Your particular code and the module code are treated the same way: as it streams into Espruino - either by entering in the console or by uploading from the editor pane or as it happens with referenced modules that get loaded from the module Web or SDCard - the code is processed and placed in Espruino's RAM. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-03-10 by CanyonCasa @allObjects thanks. You know I read that Espruino runs code on upload a couple times, but so used to conventional compiled code mindset it didn't sink in. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2016-03-07 by CanyonCasa
I find some of this info in other posts, but not a direct comparison. I'm curious as to the trades of speed and size efficiency, "public" (exposed) vs "private" (not exposed), and any other pros or cons of various ways of defining methods relative to Espruino. For example, consider the module...
And the text case use ...
Which produces the output...
I'm sure one could come up with other method notions given the "rich" flexibility of JavaScript, but for starters I'd like to know which one of these declarations makes better sense and why.
The barNone declaration is not visible or usable outside the module, but the others are. The bar declaration is explicitly exposed to the user and makes for a more verbose listing of foo, but appears to minimize better. It would also seem to be faster as the object does not need to look down the prototype chain. However, this approach does not seem to be used. Is there some other downside, for example scope or code replication with additional "new" foo objects. And the prototype declarations are not explicitly visible, but still exposed to the user. What other reasons would you chose one way over another?
Beta Was this translation helpful? Give feedback.
All reactions