Test name | Executions per second |
---|---|
flatMap | 15177.9 Ops/sec |
reduce | 17209.0 Ops/sec |
var raw = new Array(250).fill({
id: 'gid://something/Something/something',
handle: 'something',
name: 'Something',
categories: ['SOMETHING'],
lastViewedAt: '2022-03-18T21:30:18Z',
isPinned: false,
});
const flatMapped = raw.flatMap(({ id, categories, handle, name, lastViewedAt, isPinned }) => {
if (categories.length === 0) {
return [];
}
return {
category: categories[0],
handle,
url: "https://google.com/",
name,
lastViewedAt: lastViewedAt == null ? null : new Date(lastViewedAt),
author: "someone",
isPinned,
type: "something",
gid: id,
};
});
const reduced = raw.reduce(function (array, { id, categories, handle, name, lastViewedAt, isPinned }) {
if (categories.length === 0) {
return array;
}
array.push({
category: categories[0],
handle,
url: "https://google.com/",
name,
lastViewedAt: lastViewedAt == null ? null : new Date(lastViewedAt),
author: "someone",
isPinned,
type: "something",
gid: id,
});
return array;
}, []);