Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Move To First
(version: 0)
Comparing performance of:
Original Code vs Updated Code
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var wallets = [ { currency: { id: "USD" } }, { currency: { id: "IDR" } }, { currency: { id: "SGD" } }, ]
Tests:
Original Code
const moveCurrencyToFirst = (wallets, currencyId) => { const currencyIdx = wallets.findIndex( (wallet) => wallet.currency.id === currencyId ); const tempWallets = [...wallets]; if (currencyIdx !== -1) { const currWallet = wallets[currencyIdx]; tempWallets.splice(currencyIdx); tempWallets.unshift(currWallet); } return tempWallets; }; console.log(moveCurrencyToFirst(wallets, "SGD"))
Updated Code
const moveCurrencyToFirst = (wallets, currencyId) => { const moveToFirst = wallets.find( (wallet) => wallet.currency.id === currencyId ); if(!moveToFirst) return wallets; const rest = wallets.filter( (wallet) => wallet.currency.id !== currencyId ); return [ moveToFirst, ...rest ]; }; console.log(moveCurrencyToFirst(wallets, "SGD"))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Original Code
Updated Code
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Original Code
267819.4 Ops/sec
Updated Code
271817.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark data to understand what's being tested. **Benchmark Definition** The `Script Preparation Code` section defines a JavaScript array of wallet objects, each containing an object with a `currency.id` property. This represents a set of wallets that will be used as input for the benchmarks. **Options Compared** Two versions of the same function are being compared: 1. **Original Code**: The original implementation uses `findIndex()` to find the index of the wallet with the specified currency ID, and then uses `splice()` and `unshift()` to move the wallet to the first position. 2. **Updated Code**: The updated implementation uses `find()` to find the wallet with the specified currency ID, and then uses `filter()` to remove all wallets except for the one with the specified ID. **Pros and Cons** * **Original Code**: + Pros: May be faster since it only iterates over the array once using `findIndex()`. + Cons: Can be slower if the array is large, since `splice()` can lead to performance issues. * **Updated Code**: + Pros: More concise and easier to read, as it uses more modern JavaScript features like `find()` and `filter()`. + Cons: May be slower due to the additional function calls. **Library** There doesn't appear to be any external library being used in this benchmark. However, some browsers may have built-in functions or optimizations that could affect the performance of these implementations. **Special JavaScript Features/Syntax** The benchmark uses modern JavaScript features like: * **Arrow functions**: The `moveToFirst` function is defined using an arrow function, which is a concise way to define small functions. * **Spread operator**: The `...rest` syntax is used to spread the elements of an array. **Other Considerations** When comparing these two implementations, it's essential to consider factors like: * **Array size**: How many wallets are in the input array? This can affect performance, as larger arrays may lead to slower results. * **Browser and version**: Different browsers and versions may have varying optimizations or implementation details that could impact performance. **Alternatives** Other alternatives for implementing this functionality might include: * Using `indexOf()` instead of `findIndex()` * Using `slice()` instead of `splice()` and `unshift()` * Using a more efficient data structure, like a hash table, to store the wallet objects * Using a different programming language or framework that provides better performance optimizations for this specific task.
Related benchmarks:
circleTest
CircleSmallTest
asdasdasdasd
Object keys as array - remove first
Object.values vs _.values vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?