Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sortby vs flatmap
(version: 0)
Comparing performance of:
flatMap & find vs sortBy & findByIndex
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var methods = [ {id: "insure", value: 19}, {id: "denial", value: 19}, {id: "quaint", value: 19}, {id: "proper", value: 19}, {id: "theory", value: 33}, {id: "supply", value: 33}, {id: "double", value: 33}, {id: "agenda", value: 33}, {id: "delete", value: 17}, {id: "estate", value: 17}, {id: "smooth", value: 17}, {id: "border", value: 17}, {id: "ensure", value: 19}, {id: "finish", value: 19}, {id: "manner", value: 19}, {id: "exotic", value: 19}, {id: "extent", value: 19}, {id: "layout", value: 19}, {id: "decade", value: 15}, {id: "define", value: 15}, {id: "morale", value: 17}, {id: "engine", value: 17}, {id: "detail", value: 17}, {id: "stroll", value: 17}, {id: "season", value: 17}, {id: "sphere", value: 17}, {id: "rotate", value: 17}, {id: "endure", value: 17}, { id: "morsel", value: 13}, {id: "harbor", value: 15}, {id: "topple", value: 15}, {id: "tumble", value: 15}, {id: "change", value: 15}, {id: "review", value: 15}, {id: "dealer", value: 15}, {id: "mutter", value: 15}, {id: "sketch", value: 15}, {id: "volume", value: 15}, {id: "person", value: 15}, { id: "locate", value: 13}, { id: "remain", value: 13}, { id: "polite", value: 13}, { id: "helmet", value: 13}, { id: "camera", value: 13}, { id: "please", value: 13}, { id: "planet", value: 13}, { id: "height", value: 13}, { id: "valley", value: 13}, { id: "peanut", value: 13}, { id: "treaty", value: 13}, ]; var orderOfMethods = [ "locate", "remain", "polite", "helmet", "camera", "please", "planet", "height", "valley", "peanut", "treaty", "morsel", "harbor", "topple", "tumble", "change", "review", "dealer", "mutter", "sketch", "volume", "person", "decade", "define", "morale", "engine", "detail", "stroll", "season", "sphere", "rotate", "endure", "delete", "estate", "smooth", "border", "ensure", "finish", "manner", "exotic", "extent", "layout", "insure", "denial", "quaint", "proper", "theory", "supply", "double", "agenda" ]
Tests:
flatMap & find
orderOfMethods.flatMap((id) => { const method = methods.find((p) => p.id === id) return method || [] })
sortBy & findByIndex
_.sortBy(methods, x => orderOfMethods.findIndex(y => x.id === y));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
flatMap & find
sortBy & findByIndex
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!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and explain what is being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition:** The benchmark consists of two test cases: 1. "flatMap & find" 2. "sortBy & findByIndex" Both tests use a set of predefined methods (var `methods`) and an array of order-of-methods values (var `orderOfMethods`). **What is being tested?** In both tests, the goal is to compare the performance of two different approaches: * Approach 1: Using the `flatMap` method with a callback function that finds an element in the `methods` array using `find()` and returns its value. * Approach 2: Using the `sortBy` function from the Lodash library (more on this later) to sort the `methods` array based on the order-of-methods values. The tests measure the time taken by each approach to execute. **Options compared:** The two approaches are: 1. **flatMap & find**: This approach uses a combination of `flatMap` and `find()` methods to iterate through the `orderOfMethods` array and retrieve the corresponding element from the `methods` array. 2. **sortBy & findByIndex**: This approach uses the `sortBy` function to sort the `methods` array based on the order-of-methods values, and then uses a callback function with `find()` to retrieve the sorted elements. **Pros and Cons:** 1. **flatMap & find**: * Pros: + Simple and straightforward implementation. + Easy to understand and maintain. * Cons: + May be less efficient than using `sortBy` due to repeated searches in the `methods` array. 2. **sortBy & findByIndex**: * Pros: + Can be more efficient if the `methods` array is large, as it only requires a single pass through the data. + Allows for easy sorting and indexing of the elements. * Cons: + May require additional setup with Lodash library. + The code can become complex if not properly optimized. **Lodash library:** The `sortBy` function is part of the Lodash library, which provides a set of utility functions for functional programming in JavaScript. In this benchmark, the `sortBy` function is used to sort the `methods` array based on the order-of-methods values. Other considerations: * **Performance**: The performance difference between these two approaches can be significant, especially when dealing with large datasets. * **Code complexity**: The `sortBy & findByIndex` approach requires more code and setup compared to the simple `flatMap & find` approach. * **Library usage**: Using an external library like Lodash can add overhead in terms of loading time and memory usage. Overall, the choice between these two approaches depends on the specific requirements of your use case, such as performance, simplicity, and maintainability.
Related benchmarks:
sortBy performance2
lodash vs es6 in sort method
indexOf lookup vs initial map then sort
filter + map vs flatMap
Comments
Confirm delete:
Do you really want to delete benchmark?