257b0a43ac
Design doc: https://docs.google.com/document/d/1rxM3sDd-ZiOLznqw7MvYraulAPWJSVqC_CztO4YpUTQ/edit Change-Id: I471ff31f32b7bdd22cb03005c1dcc18aa485ad77 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3313793 Auto-Submit: Jianxiao Lu <jianxiao.lu@intel.com> Reviewed-by: Camillo Bruni <cbruni@chromium.org> Commit-Queue: Shiyu Zhang <shiyu.zhang@intel.com> Cr-Commit-Position: refs/heads/main@{#78428}
67 lines
1.7 KiB
JavaScript
67 lines
1.7 KiB
JavaScript
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
export function delay(time) {
|
|
return new Promise(resolver => setTimeout(resolver, time));
|
|
}
|
|
|
|
export function defer() {
|
|
let resolve_func, reject_func;
|
|
const p = new Promise((resolve, reject) => {
|
|
resolve_func = resolve;
|
|
reject_func = resolve;
|
|
});
|
|
p.resolve = resolve_func;
|
|
p.reject = reject_func;
|
|
return p;
|
|
}
|
|
|
|
export class Group {
|
|
constructor(key, id, parentTotal, entries) {
|
|
this.key = key;
|
|
this.id = id;
|
|
this.entries = entries;
|
|
this.length = entries.length;
|
|
this.parentTotal = parentTotal;
|
|
}
|
|
|
|
get percent() {
|
|
return this.length / this.parentTotal * 100;
|
|
}
|
|
|
|
add() {
|
|
this.length++;
|
|
}
|
|
|
|
addEntry(entry) {
|
|
this.length++;
|
|
this.entries.push(entry);
|
|
}
|
|
}
|
|
|
|
export function groupBy(array, keyFunction, collect = false) {
|
|
if (array.length === 0) return [];
|
|
if (keyFunction === undefined) keyFunction = each => each;
|
|
const keyToGroup = new Map();
|
|
const groups = [];
|
|
const sharedEmptyArray = [];
|
|
let id = 0;
|
|
// This is performance critical, resorting to for-loop
|
|
for (let each of array) {
|
|
const key = keyFunction(each);
|
|
let group = keyToGroup.get(key);
|
|
if (group !== undefined) {
|
|
collect ? group.addEntry(each) : group.add();
|
|
continue;
|
|
}
|
|
let entries = collect ? [each] : sharedEmptyArray;
|
|
group = new Group(key, id++, array.length, entries);
|
|
groups.push(group);
|
|
keyToGroup.set(key, group);
|
|
}
|
|
// Sort by length
|
|
return groups.sort((a, b) => b.length - a.length);
|
|
}
|
|
|
|
export * from '../js/helper.mjs' |