Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

JavaScript optimization guidelines for FitBit devices

Composed my answers on different topics to the document with guidelines. If you ever encountered out of memory exception, it worth reading. Enjoy.

https://github.com/gaperton/ionic-views/blob/master/docs/optimization-guidelines.md

Best Answer
5 REPLIES 5

That's amazing! Thank you so much for providing this!

Best Answer
0 Votes

Excellent article!

 

I have a couple of questions regarding the last part 'Use functions instead of hashmaps and arrays to save heap memory'. Although you are saving memory by using the function, doesn't the function itself take up memory? Just wondering if the function itself would be larger than 64 bytes?

 

This leads me to my second question. Does code sit in the same 64K as the heap? If not, where does the code sit and how large can it be?

Best Answer

> Does code sit in the same 64K as the heap?

That's the good question. There's nothing said about that in the docs, and I assumed that the heap is separate. But let's check.


14010 - is the maximum x length while the simulator doesn't throw out of memory exception for this code.

const x = new Float32Array( 14010 );

const y = {
  a0 : "sasasasa",
  a1 : "sasasasa",
  a2 : "sasasasa",
  a3 : "sasasasa",
  a4 : "sasasasa",
  a5 : "sasasasa",
  length : "sasasasa"
}

console.log( x.length, y.length );


When we change the code like this - it throws "out of memory".

const x = new Float32Array( 14010 );

function y( k ){
  switch( k ){
    case 'a0' : return "sasasasa";
    case 'a1' : return "sasasasa";
    case 'a2' : return "sasasasa";
    case 'a3' : return "sasasasa";
    case 'a4' : return "sasasasa";
    case 'a5' : return "sasasasa";
    case 'length' : return "sasasasa";
  }
}

console.log( x.length, y.length );

Apparently, it's not that simple, and this "optimization" make things worse. It's either the code is located in the heap or the heap with a code segment share the same memory quote. And that was the single thing I didn't really check myself 🙂

Will change an article accordingly, thanks.

Best Answer

Updated the article. Now it's documented as an antipattern, which you need to avoid 🙂 Surprise. @Mark-D, thanks a lot!

https://github.com/gaperton/ionic-views/blob/master/docs/optimization-guidelines.md#do-not-use-funct...

Best Answer

Yes, now I'm pretty sure that both the code and the heap lives under the same quote, which is slightly less than 64KB.

https://dev.fitbit.com/build/reference/device-api/system/

import { memory } from "system";
console.log("JS memory: " + memory.js.used + "/" + memory.js.total);
Best Answer