ic-model.mjs 1.61 KB
Newer Older
zeynepCankara's avatar
zeynepCankara committed
1 2 3 4
// 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.

5
import CustomIcProcessor from "./ic-processor.mjs";
zeynepCankara's avatar
zeynepCankara committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

// For compatibility with console scripts:
print = console.log;

export class Group {
  constructor(property, key, entry) {
    this.property = property;
    this.key = key;
    this.count = 1;
    this.entries = [entry];
    this.percentage = undefined;
    this.groups = undefined;
  }

  add(entry) {
    this.count++;
    this.entries.push(entry)
  }

  createSubGroups() {
    this.groups = {};
27 28
    for (let i = 0; i < CustomIcProcessor.kProperties.length; i++) {
      let subProperty = CustomIcProcessor.kProperties[i];
zeynepCankara's avatar
zeynepCankara committed
29 30 31 32 33 34 35 36 37 38 39 40
      if (this.property == subProperty) continue;
      this.groups[subProperty] = Group.groupBy(this.entries, subProperty);
    }
  }

  static groupBy(entries, property) {
    let accumulator = Object.create(null);
    let length = entries.length;
    for (let i = 0; i < length; i++) {
      let entry = entries[i];
      let key = entry[property];
      if (accumulator[key] == undefined) {
41
        accumulator[key] = new Group(property, key, entry);
zeynepCankara's avatar
zeynepCankara committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
      } else {
        let group = accumulator[key];
        if (group.entries == undefined) console.log([group, entry]);
        group.add(entry)
      }
    }
    let result = [];
    for (let key in accumulator) {
      let group = accumulator[key];
      group.percentage = Math.round(group.count / length * 100 * 100) / 100;
      result.push(group);
    }
    result.sort((a, b) => {return b.count - a.count});
    return result;
  }
57

zeynepCankara's avatar
zeynepCankara committed
58
}