You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Ok, thanks for trying to narrow this down. I think really I'd need to see an example of this being used, but nothing looks that bad space-wise, unless what you're pushing into it ends up including a bunch of extra info (maybe via the scope it was defined in).

    Honestly though, this is not the way to write code for Espruino. It's going to be so slow! By using Queue you're basically just reimplementing what Array does but adding a whole extra layer of abstraction which is eating memory and CPU time.

    I mean, you look at isEmpty, it's calling this.any() which calls _this.length which is a getter which calls this.internalArray.length. It's just a nightmare - it's like someone wrote the code specifically to waste CPU cycles - even on Node.js it's going to suck.

    On Espruino as far as I can see you could just use Array directly, and if you really need to limit the size of the queue you could override .push to just call splice after it to limit the length.

    Just a quick example:

    var q = new Queue(20);
    t=getTime();for (var i=0;i<20;i++)q.push(i);print(getTime()-t);
    // 0.082 seconds
    var q = [];
    t=getTime();for (var i=0;i<20;i++)q.push(i);print(getTime()-t);
    // 0.0102 seconds
    

    So it's 8x slower than just using an Array, and honestly I'm amazed it's that good.

  • So it's 8x slower than just using an Array, and honestly I'm amazed
    it's that good.

    At first I did just use an array but I need to limit the size otherwise the mem is very quickly consumed (think wanting to sample gps data, but only recent as you want to reduce noise). You are speed testing two different things. You are saying pushing to an array is quicker than pushing to an array and then pruning it. Which is obviously true. If you have a quicker way of pushing and then resizing that would be more constructive.

    On Espruino as far as I can see you could just use Array directly, and
    if you really need to limit the size of the queue you could override
    .push to just call splice after it to limit the length.

    I am literally doing the same thing as splice after push (via shift). Originally I extended Array rather than having an internal array variable but the espruino compiler isn't releasing memory when splice or shift.

    I mean, you look at isEmpty, it's calling this.any() which calls
    _this.length which is a getter which calls this.internalArray.length. It's just a nightmare - it's like someone wrote the code specifically
    to waste CPU cycles - even on Node.js it's going to suck
    If I want to know if my array is empty I need to do a check in my code. I still need to call array.length. I could repeat this code every time I want to check, it wouldn't speed up the processing, or I can add an additional layer which can be individually unit tested and optimised further in future.

    Ultimately you might save some cpu time with large chunks of repeated code. It will limit you from creating good stable apps which are easy to maintain and extend. Most of the apps in the current app library are not extendable or maintainable. You need to be able to split the code into small segments to unit test. Otherwise you are taking a step back to pre object orientated test driven development.

About

Avatar for Gordon @Gordon started