Duplicate Priority Filtering

Benchmark created on


Description

Filtering the highest priority duplicate from a list of duplicate json objects (duplicated on primary key)

Setup

var items = [
  { primaryKey: "test1", priority: 3 },
  { primaryKey: "test2", priority: 3 },
  { primaryKey: "test3", priority: 3 },
  { primaryKey: "test1", priority: 2 },
  { primaryKey: "test1", priority: 3 },
  { primaryKey: "test2", priority: 3 },
  { primaryKey: "test3", priority: 3 },
  { primaryKey: "test1", priority: 2 },
  { primaryKey: "test1", priority: 3 },
  { primaryKey: "test2", priority: 3 },
  { primaryKey: "test3", priority: 3 },
  { primaryKey: "test1", priority: 2 },
  { primaryKey: "test1", priority: 3 },
  { primaryKey: "test2", priority: 3 },
  { primaryKey: "test3", priority: 3 },
  { primaryKey: "test1", priority: 2 },
  { primaryKey: "test1", priority: 3 },
  { primaryKey: "test2", priority: 3 },
  { primaryKey: "test3", priority: 3 },
  { primaryKey: "test1", priority: 2 },
  { primaryKey: "test1", priority: 1 }
];

Test runner

Ready to run.

Testing in
TestOps/sec
Double Sort
//Sort first (priority then primary key), then check if primary key has changed in loop, then write to array
const highest = [];

items.sort((a, b) => {
  const keyA = a.primaryKey;
  const keyB = b.primaryKey;
  const valueA = a.priority;
  const valueB = b.priority;

  // Sort by value (priority) first
  if (valueA !== valueB) {
    return valueA - valueB;
  }

  // If values match, sort by key name
  return keyA.localeCompare(keyB);
});

let keyGroup;

for (const obj of items) {
  if (keyGroup != obj.primaryKey){
    highest.push(obj);
    keyGroup = obj.primaryKey;
    }
}

console.log(highest);
ready
ChatGPT Oneshot
//Loop over items, check if key already exists, then check if current item is higher or lower, then write to dict
// Create a map of key → max priority
const map = {};

for (const obj of items) {
  const key = obj.primaryKey;
  const value = obj.priority;

  if (!(key in map) || value > map[key]) {
    map[key] = value;   // keep highest priority
  }
}

// Convert back to array of objects in the new structure
const highest = Object.entries(map).map(([key, value]) => ({
  primaryKey: key,
  priority: value
}));

console.log(highest);
ready
Sort first, compare, then write to dict
//Sort items by priority, loop over items and check if key is in dict, otherwise write key to dict
// Create a map of key → max priority
const map = {};

items.sort((a, b) => {
  const valueA = a.priority;
  const valueB = b.priority;

  // Sort by value (priority) first
  if (valueA !== valueB) {
    return valueA - valueB;
  }
});

for (const obj of items) {
  const key = obj.primaryKey;
  const value = obj.priority;

  if (!(key in map)) {
    map[key] = value;   // keep first primary key instance (Highest priority appears first due to sort) 
  }
}

// Convert back to array of objects in the new structure
const highest = Object.entries(map).map(([key, value]) => ({
  primaryKey: key,
  priority: value
}));

console.log(highest);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.