Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test 2 function in vanilla js
(version: 0)
Comparing performance of:
reduce and map vs reduce and findIndex
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [ { name: "Jules", age: 2, famille: "adam" }, { name: "John", famille: "dupont" }, { name: "Julie", famille: "dupont" }, { name: "Juliette" } ];
Tests:
reduce and map
const result = Object.values( array.reduce((acc, obj) => { if (obj.hasOwnProperty("famille")) { if (!acc.hasOwnProperty(obj.famille)) { acc[obj.famille] = { prenom: [], famille: obj.famille }; } acc[obj.famille].prenom.push(obj.name); } return acc; }, {}) ).map((group) => { return { prenom: group.prenom, famille: group.famille }; }); console.log(result);
reduce and findIndex
const result = array.reduce((acc, curr) => { let index = acc.findIndex((item) => item.famille === curr.famille); if (index !== -1) { acc[index].prenom.push(curr.name); } else { acc.push({ prenom: [curr.name], famille: curr.famille }); } return acc; }, []); console.log(result);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce and map
reduce and findIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
Array.from 1e6 vs 1e3
Array.from vs Array spread with mapping of values
sample_benchmark
Test array and unshift
array.length = 0 vs []
Comments
Confirm delete:
Do you really want to delete benchmark?