From 41d0dc6ffc7e7ccdda1d79fe879b797b5b47a02a Mon Sep 17 00:00:00 2001 From: Lykin <137850705+tiny-craft@users.noreply.github.com> Date: Mon, 25 Mar 2024 14:57:10 +0800 Subject: [PATCH] perf: keys containing numbers are sorted by size first (#198) --- frontend/src/objects/redisNodeItem.js | 48 ++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/frontend/src/objects/redisNodeItem.js b/frontend/src/objects/redisNodeItem.js index c1f5173..7c03024 100644 --- a/frontend/src/objects/redisNodeItem.js +++ b/frontend/src/objects/redisNodeItem.js @@ -1,4 +1,4 @@ -import { isEmpty, remove, size, sortedIndexBy, sumBy } from 'lodash' +import { isEmpty, remove, size, sumBy } from 'lodash' import { ConnectionType } from '@/consts/connection_type.js' /** @@ -68,6 +68,46 @@ export class RedisNodeItem { }) } + /** + * compare two items to determine the sort order + * @param {*} a + * @param {*} b + * @return {number} + */ + _sortingCompare(a, b) { + if (a.type !== b.type) { + return a.type - b.type + } + const isANum = isNaN(a.label) + const isBNum = isNaN(b.label) + if (!isANum && !isBNum) { + return parseInt(a.label, 10) - parseInt(b.label, 10) + } else if (!isANum) { + return -1 + } else if (!isBNum) { + return 1 + } + return a.label.localeCompare(b.label) + } + + /** + * calculate insert sorted index + * @param {[]} arr + * @param {*} item + * @return {number} + */ + _sortedIndex(arr, item) { + for (let i = 0; i < arr.length; i++) { + const cmpResult = this._sortingCompare(arr[i], item) + if (cmpResult > 0) { + return i + } else if (cmpResult === 0) { + return i + 1 + } + } + return arr.length + } + /** * sort all node item's children and calculate keys count * @param skipSort skip sorting children @@ -98,9 +138,7 @@ export class RedisNodeItem { } sortChildren() { - this.children.sort((a, b) => { - return a.key > b.key ? 1 : -1 - }) + this.children.sort(this._sortingCompare) } /** @@ -112,7 +150,7 @@ export class RedisNodeItem { if (!!!sorted) { this.children.push(child) } else { - const idx = sortedIndexBy(this.children, child, 'key') + const idx = this._sortedIndex(this.children, child) this.children.splice(idx, 0, child) } }