Skip to content

Instantly share code, notes, and snippets.

@chuntaro
Created March 10, 2020 03:52
Show Gist options
  • Select an option

  • Save chuntaro/d53376707e27187635018a7d2ad5f9bb to your computer and use it in GitHub Desktop.

Select an option

Save chuntaro/d53376707e27187635018a7d2ad5f9bb to your computer and use it in GitHub Desktop.
JavaScript で重複削除のテストコード
let array = [...Array(50)].map(_ => Math.floor(Math.random() * 20));
let a = array.filter(function (x, i, self) {
return self.indexOf(x) === i;
});
let b = Array.from(new Set(array));
console.log(a, b);
let loopCount = 1000000;
console.time("filter");
for (let i = 0; i < loopCount; ++i) {
let a = array.filter(function (x, i, self) {
return self.indexOf(x) === i;
});
}
console.timeEnd("filter");
console.time("set");
for (let i = 0; i < loopCount; ++i) {
let a = Array.from(new Set(array));
}
console.timeEnd("set");
$ node duplicate-removal.js
[
3, 9, 17, 11, 16, 14, 4,
6, 2, 12, 15, 7, 18, 1,
13, 19, 10, 8, 5
] [
3, 9, 17, 11, 16, 14, 4,
6, 2, 12, 15, 7, 18, 1,
13, 19, 10, 8, 5
]
filter: 1184.350ms
set: 1253.351ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment