2

I have an array that is constantly updated and accordingly it is necessary to update its grouping. Example of an array:

[
 {
    "price": 2419.62,
    "amount": 0.0266
  },
  {
    "price": 1927.52,
    "amount": 0.0217
  },
  ...
]

I tried different options. At the moment this option is the fastest:

      const points = [
           {
              "price": 2419.62,
              "amount": 0.0266
            },
            {
              "price": 1927.52,
              "amount": 0.0217
            },
            ...
          ];
      const range = 500;
      const spread = 1800;
      const countGroup = 250;
      const sizeUnitGroup = range / countGroup;
      const groups = {};
      for (let i = 0;  i < countGroup; i++){
        groups[i] = [];
        try {
          points.forEach((item, id) => {
            if (item.price > spread + (i*sizeUnitGroup) && item.price <= spread + (i*sizeUnitGroup + sizeUnitGroup)){
              groups[i].push(item);
              points.splice(id, 1);
            }
            if (item.price > (spread + (i*sizeUnitGroup + sizeUnitGroup))) throw BreakException;
          });
        } catch (e) {
        }
      }

But even so, this function works for too long. Any ideas how this can be optimized?

5
  • 1
    What is the logic of the grouping? Commented Feb 24, 2018 at 16:00
  • Please post an example update for your example array (how are things regrouped?). Commented Feb 24, 2018 at 16:00
  • 1
    That's a lot of iterating and splicing. Wouldn't it be better to just iterate the points array and figure out which group each one should go into? Commented Feb 24, 2018 at 16:01
  • @doodlemeister You mean this is it? pastebin.com/C7kh6ba6 Commented Feb 24, 2018 at 16:07
  • Yes, did that help? It avoids the .splice(), which has to reindex the array every time. Commented Feb 24, 2018 at 16:11

1 Answer 1

3

You could calculate the interval for pushing the value to the wanted slot.

var points = [
        { price: 2419.62, amount: 0.0266 },
        { price: 1927.52, amount: 0.0217 },
        { price: 1800, amount: 0.07 },              // -1 not in result
        { price: 1800.000000000001, amount: 0.07 }, //  0
        { price: 1802, amount: 0.07 },              //  0
    ],
    range = 500,
    spread = 1800,
    countGroup = 250,
    sizeUnitGroup = range / countGroup,
    groups = {};

points.forEach((item, id) => {
    var i = Math.ceil((item.price - spread- sizeUnitGroup) / sizeUnitGroup);
    if (i >= 0 && i < countGroup) {
        groups[i] = groups[i] || [];
        groups[i].push(item);
    }
});

console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.