Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test for of and flatMap
(version: 0)
Comparing performance of:
flatmap vs for of
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateMockTeamsWithData(records) { const mockData = []; for (let i = 0; i < records; i++) { const team = { teamName: `Team ${i}`, channels: [] }; // Generate a random number of channels per team (1 to 5) const channelCount = Math.floor(Math.random() * 5) + 1; for (let j = 0; j < channelCount; j++) { const channel = { channelName: `Channel ${j}`, messages: [] }; // Generate a random number of messages per channel (1 to 10) const messageCount = Math.floor(Math.random() * 10) + 1; for (let k = 0; k < messageCount; k++) { const message = { positiveMessageCount: Math.floor(Math.random() * 5), negativeMessageCount: Math.floor(Math.random() * 5), neutralMessageCount: Math.floor(Math.random() * 5), user: { id: `user${Math.floor(Math.random() * 1000)}` } }; channel.messages.push(message); } team.channels.push(channel); } mockData.push(team); } return mockData; } // Usage var teamsWithData = generateMockTeamsWithData(1000);
Tests:
flatmap
function testFlatMap(teams) { const flatMessages = teams.flatMap((team) => team.channels.flatMap((channel) => channel.messages)); let totalPositiveCount = 0; let totalNegativeCount = 0; let totalNeutralCount = 0; const uniqueUserIds = new Set(); for (const message of flatMessages) { totalPositiveCount += message.positiveMessageCount; totalNegativeCount += message.negativeMessageCount; totalNeutralCount += message.neutralMessageCount; uniqueUserIds.add(message.user.id ?? ''); } return { totalPositiveCount, totalNegativeCount, totalNeutralCount, uniqueUserCount: uniqueUserIds.size }; } const result = testFlatMap(teamsWithData);
for of
// Test function function testForOfLoop(teams) { let totalPositiveCount = 0; let totalNegativeCount = 0; let totalNeutralCount = 0; const uniqueUserIds = new Set(); for (const team of teams) { for (const channel of team.channels) { for (const message of channel.messages) { totalPositiveCount += message.positiveMessageCount; totalNegativeCount += message.negativeMessageCount; totalNeutralCount += message.neutralMessageCount; uniqueUserIds.add(message.user.id ?? ''); } } } return { totalPositiveCount, totalNegativeCount, totalNeutralCount, uniqueUserCount: uniqueUserIds.size }; } const result2 = testForOfLoop(teamsWithData);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
flatmap
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
flatmap
852.2 Ops/sec
for of
1674.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition** The benchmark definition is a JavaScript function that generates mock teams with data, which is then used to test two different array iteration methods: `flatMap` and `for...of`. The function `generateMockTeamsWithData` creates an array of team objects, each with multiple channels and messages. The `testFlatMap` and `testForOfLoop` functions process the array of teams to calculate statistics about the messages. **Options Compared** Two options are compared: 1. **`flatMap`**: This method is used to flatten an array by iterating over each element and returning a new array with the results. 2. **`for...of` loop**: This is a traditional loop that iterates over each element in an array. **Pros and Cons of Each Approach** * **`flatMap`**: + Pros: Efficient, concise, and expressive way to flatten arrays. + Cons: May have performance overhead due to the creation of new arrays. * **`for...of` loop**: + Pros: Can be more readable and easier to understand for some developers. Allows for direct access to array elements using the `index` variable. + Cons: Typically slower than `flatMap`, as it involves more iterations over the array. **Other Considerations** * The benchmark uses a mock dataset generated by the `generateMockTeamsWithData` function, which ensures that the performance results are not influenced by external factors like network latency or disk I/O. * The `testForOfLoop` function has an additional loop around the `for...of` iteration, which can make it slower compared to `flatMap`. * The benchmark measures the execution time per second (`ExecutionsPerSecond`) for each test case, providing a rough estimate of performance. **Special JS Feature or Syntax** The benchmark uses the `flatMap` method and arrow functions (e.g., `() => team.channels.flatMap((channel) => channel.messages)`), which are modern JavaScript features. These features can make the code more concise and expressive but may not be supported by older browsers or environments. **Library or Framework** There is no explicit library or framework mentioned in the benchmark definition, as it appears to be a self-contained test case using only standard JavaScript features. **Alternative Approaches** Other alternative approaches for iterating over arrays include: 1. **`forEach` method**: This method iterates over each element in an array and can be used with callback functions. 2. **`map` and `reduce` methods**: These methods provide a way to process arrays by applying transformations or aggregations. 3. **Regular loops using `index` variable**: This approach involves using traditional loops with the `index` variable, similar to the `for...of` loop. These alternatives may have different performance characteristics or trade-offs in terms of readability and conciseness compared to `flatMap`.
Related benchmarks:
Fill array with random integers
Array concat vs. spread vs. push
Memory Tests
Testering
Comments
Confirm delete:
Do you really want to delete benchmark?