JSFiddle - React, Tailwind, and code Playground
JavaScript
function trincot(newSelectedModelList) {
return newSelectedModelList.filter(function ({year, name}, key) {
return !this.has(key = year + name) && this.add(key);
}, new Set());
}
function guest271314(arr) {
var res = {};
var i = 0;
do {
if (!res[JSON.stringify(arr[++i])]) {
res[JSON.stringify(arr[i])] = true;
} else {
arr.splice(i, 1);
i -= 1;
}
} while (arr[i]);
}
// Build random data set with duplicates
function randomChar() {
return 'abcdefghijklmnopqrstuvwxyz'.charAt(Math.floor(Math.random() * 26));
}
function randomYear() {
return 1990 + Math.floor(Math.random() * 26);
}
var data = [];
for (var i = 0; i < 30000; i++) {
data.push ({
name: randomChar() + randomChar(),
year: randomYear()
});
}
console.log('number of original elements: ', data.length);
console.log(' solution', 'count', 'duration');
console.log('-----------', '-----', '--------');
var arr;
// Test 1
arr = data.slice();
var start = performance.now();
arr = trincot(arr);
console.log(' trincot', arr.length, ((100000+performance.now()-start).toFixed(2)).substr(1));
// Test 2
arr = data.slice();
var start = performance.now();
guest271314(arr); // in-place, so no return value needed
console.log('guest271314', arr.length, ((100000+performance.now()-start).toFixed(2)).substr(1));