Skip to content

Commit

Permalink
* tyr to optimize some array operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Jun 6, 2022
1 parent 0ff9f36 commit 8d29907
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"loglevel": "info",
"messagebox": true,
"subscribe": "messagebox",
"eraseOnUpload": true,
"keywords": [
"charts",
"history",
Expand Down
24 changes: 12 additions & 12 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function finishAggregation(options) {
};
}
} else
if (options.result[ii].end.ts === options.result[ii].max.ts) {
if (options.result[ii].end.ts === options.result[ii].max.ts) {
// just one value in this period: start == min, max == end
if (options.result[ii].min.ts === options.result[ii].start.ts) {
options.result.splice(ii + 1, 0, {
Expand Down Expand Up @@ -368,15 +368,15 @@ function finishAggregation(options) {
options.result.splice(0, 1);
}
if (!postBorderValueRemoved) {
options.result.splice(-1, 1);
options.result.length--;
}
}
} else if (options.aggregate === 'average') {
if (options.removeBorderValues) { // we cut out the additional results
options.result.splice(0, 1);
options.averageCount.splice(0, 1);
options.result.splice(-1, 1);
options.averageCount.splice(-1, 1);
options.result.length--;
options.averageCount.length--;
}
for (let k = options.result.length - 1; k >= 0; k--) {
if (options.result[k].val.ts) {
Expand Down Expand Up @@ -490,7 +490,7 @@ function finishAggregation(options) {
}
if (options.removeBorderValues) { // we cut out the additional results
options.result.splice(0, 1);
options.result.splice(-1, 1);
options.result.length--;
}
for (let j = options.result.length - 1; j >= 0; j--) {
if (options.result[j].val === null) {
Expand All @@ -502,8 +502,8 @@ function finishAggregation(options) {
if (options.removeBorderValues) { // we cut out the additional results
options.result.splice(0, 1);
options.quantileDatapoints.splice(0, 1);
options.result.splice(-1, 1);
options.quantileDatapoints.splice(-1, 1);
options.result.length--
options.quantileDatapoints.length--;
}
for (let k = options.result.length - 1; k >= 0; k--) {
if (options.result[k].val.ts) {
Expand All @@ -521,7 +521,7 @@ function finishAggregation(options) {
} else {
if (options.removeBorderValues) { // we cut out the additional results
options.result.splice(0, 1);
options.result.splice(-1, 1);
options.result.length--;
}
for (let j = options.result.length - 1; j >= 0; j--) {
if (options.result[j].val.ts) {
Expand Down Expand Up @@ -680,7 +680,7 @@ function beautify(options) {
}
} else if (options.aggregate === 'none') {
if (options.count && options.result.length > options.count) {
options.result = options.result.slice(-options.count);
options.result.splice(0, options.result.length - options.count);
}
}

Expand Down Expand Up @@ -725,7 +725,7 @@ function sendResponse(adapter, msg, options, data, startTime) {
beautify(options);

if (options.aggregate === 'none' && options.count && options.result.length > options.count) {
options.result = options.result.slice(-options.count);
options.result.splice(0, options.result.length - options.count);
}
aggregateData.result = options.result;
} else {
Expand Down Expand Up @@ -789,7 +789,7 @@ function sendResponseCounter(adapter, msg, options, data, startTime) {
}

if (data[data.length - 2].ts === options.end) {
data.splice(-1, 1);
data.length--;
}

const veryLast = data[data.length - 1];
Expand All @@ -798,7 +798,7 @@ function sendResponseCounter(adapter, msg, options, data, startTime) {
// interpolate from end time to last
if (options.end < veryLast.ts) {
const val = beforeLast.val + (veryLast.val - beforeLast.val) * ((options.end - beforeLast.ts) / (veryLast.ts - beforeLast.ts));
data.splice(-1, 1);
data.length--;
data.push({ts: options.end, val, i: true});
}

Expand Down
12 changes: 6 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ function getHistory(msg) {
if (isFull && cacheData.length) {
cacheData = cacheData.sort(sortByTs);
if (options.count && cacheData.length > options.count && options.aggregate === 'none') {
cacheData = cacheData.slice(-options.count);
cacheData.splice(0, cacheData.length - options.count);
debugLog && adapter.log.debug(`${options.logId} cut cacheData to ${options.count} values`);
}
adapter.log.debug(`${options.logId} Send: ${cacheData.length} values in: ${Date.now() - startTime}ms`);
Expand All @@ -1280,17 +1280,17 @@ function getHistory(msg) {
cacheData = cacheData.sort(sortByTs);
options.result = cacheData;
if (options.count && options.result.length > options.count && options.aggregate === 'none' && !options.returnNewestEntries) {
let cutPoint = 0;
if (options.start) {
for (let i = 0; i < options.result.length; i++) {
if (options.result[i].ts < options.start) {
options.result.splice(i, 1);
i--;
} else {
if (options.result[i].ts >= options.start) {
cutPoint = i;
break;
}
}
}
options.result = options.result.slice(0, options.count);
cutPoint > 0 && options.result.splice(0, cutPoint);
options.result.length = options.count;
debugLog && adapter.log.debug(`${options.logId} pre-cut data to ${options.count} oldest values`);
}
if (options.debugLog) {
Expand Down

0 comments on commit 8d29907

Please sign in to comment.