Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
removeOwnOrdersFromBook performance using strings vs using numbers
(version: 0)
Comparing performance of:
removeOwnOrdersFromBook using string orderBook and string orders vs removeOwnOrdersFromBook using numeric orderBook and numeric orders
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var fictStringOrderBook = { market: 'XRP-EUR', timestamp: Date.now(), nonceCW: 0, source: 'REST', bids: [ ['0.18399', '3052.423937'], ['0.18381', '3222.189522'], ['0.1838', '1913.507669'], ['0.18377', '3289.219013'], ['0.18373', '2019.409876'] ], asks: [ ['0.184', '1534.998073'], ['0.18403', '2633.829475'], ['0.18412', '2452.407282'], ['0.18416', '2430.626048'], ['0.1842', '2043.582177'] ] } var fictNumericOrderBook = { market: 'XRP-EUR', timestamp: Date.now(), nonceCW: 0, source: 'REST', bids: [ [0.18399, 3052.423937], [0.18381, 3222.189522], [0.1838, 1913.507669], [0.18377, 3289.219013], [0.18373, 2019.409876] ], asks: [ [0.184, 1534.998073], [0.18403, 2633.829475], [0.18412, 2452.407282], [0.18416, 2430.626048], [0.1842, 2043.582177] ] } var fictBaseMarketMakerOrder = { market: 'XRP-EUR', created: +new Date('2020-01-01T00:00:01.1Z'), updated: +new Date('2020-01-01T00:00:01.1Z'), status: 'new', orderType: 'limit', onHold: '0', onHoldCurrency: 'EUR', filledAmount: '0', filledAmountQuote: '0', feePaid: '0', feeCurrency: 'EUR', fills: [], selfTradePrevention: 'cancelNewest', visible: true, timeInForce: 'GTC', postOnly: true } var ownStringOrders = { bid: [ { order: { ...fictBaseMarketMakerOrder, orderId: 'd1ff48b1-914e-4d5a-850c-f9d97c734aac', side: 'buy', amount: '312.507669', amountRemaining: '312.507669', price: '0.18381' }, expectedAmountOld: null, expectedAmount: 312.507669, doNotChangeUntil: Date.now() } ], ask: [ { order: { ...fictBaseMarketMakerOrder, orderId: '0865c62d-56e8-44f9-b929-38f79d51956f', side: 'sell', amount: '1534.998073', amountRemaining: '1534.998073', price: '0.184' }, expectedAmountOld: null, expectedAmount: 1534.998073, doNotChangeUntil: Date.now() } ] } var ownNumericOrders = { bid: [ { order: { ...fictBaseMarketMakerOrder, orderId: 'd1ff48b1-914e-4d5a-850c-f9d97c734aac', side: 'buy', amount: 312.507669, amountRemaining: 312.507669, price: 0.18381 }, expectedAmountOld: null, expectedAmount: 312.507669, doNotChangeUntil: Date.now() } ], ask: [ { order: { ...fictBaseMarketMakerOrder, orderId: '0865c62d-56e8-44f9-b929-38f79d51956f', side: 'sell', amount: 1534.998073, amountRemaining: 1534.998073, price: 0.184 }, expectedAmountOld: null, expectedAmount: 1534.998073, doNotChangeUntil: Date.now() } ] } function round(number, decimals = 0) { const power = 10 ** decimals return Math.round(number * power) / power } function n(value, decimals = 11) { return round(Number(value), decimals) || 0 } function s(value) { return (typeof value === 'number' ? numberToString(value) : `${value}`) } /** * Convert number to string, with possibility to define decimal precision. * When supplying a number >= 1e+21, the scientific notation is returned as a string. */ function numberToString(n, decimals) { return n < 1e-6 // JS scientific notation kicks in for numbers >= 1e+21 or < 1e-6. ? n.toFixed(decimals).replace(/\.?0+$/, '') // Remove trailing zeros. : `${n}` } function toQuantityString(amount) { return s(n(amount)) } function removeOwnOrdersFromStringBook(book, ownOrders) { return { bids: removeOwnOrdersFromBookStringSide(book.bids, ownOrders.bid), asks: removeOwnOrdersFromBookStringSide(book.asks, ownOrders.ask) } } function removeOwnOrdersFromNumericBook(book, ownOrders) { return { bids: removeOwnOrdersFromBookNumericSide(book.bids, ownOrders.bid), asks: removeOwnOrdersFromBookNumericSide(book.asks, ownOrders.ask) } } function removeOwnOrdersFromBookStringSide(bookSide, ownOrdersForSide) { return bookSide.reduce(function(acc, currentLevel) { const [curPriceString, curQuantityString] = currentLevel const curPrice = n(curPriceString) const curQuantity = n(curQuantityString) const totalOwnOrderQuantityForPrice = ownOrdersForSide.reduce( function(acc, { order }) { return order && curPrice === n(order.price) ? acc + n(order.amountRemaining) : acc }, 0 ) const remainingQuantity = curQuantity - totalOwnOrderQuantityForPrice if (remainingQuantity > 0) { acc.push([curPriceString, toQuantityString(remainingQuantity)]) } return acc }, []) } function removeOwnOrdersFromBookNumericSide(bookSide, ownOrdersForSide) { return bookSide.reduce(function(acc, currentLevel) { const [curPrice, curQuantity] = currentLevel const totalOwnOrderQuantityForPrice = ownOrdersForSide.reduce( function(acc, { order }) { return order && curPrice === order.price ? acc + order.amountRemaining : acc }, 0 ) const remainingQuantity = curQuantity - totalOwnOrderQuantityForPrice if (remainingQuantity > 0) { acc.push([curPrice, remainingQuantity]) } return acc }, []) }
Tests:
removeOwnOrdersFromBook using string orderBook and string orders
for (var i = 0; i < 1000; i++) { removeOwnOrdersFromStringBook(fictStringOrderBook, ownStringOrders) }
removeOwnOrdersFromBook using numeric orderBook and numeric orders
for (var i = 0; i < 1000; i++) { removeOwnOrdersFromNumericBook(fictNumericOrderBook, ownNumericOrders) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
removeOwnOrdersFromBook using string orderBook and string orders
removeOwnOrdersFromBook using numeric orderBook and numeric orders
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):
It looks like we have a JavaScript benchmarking scenario here! To analyze the code, I'll need to identify some patterns and assumptions: 1. The `removeOwnOrdersFromBook` function is designed to remove own orders from an order book. 2. There are two implementations of this function: one that takes a string representation of the order book (`removeOwnOrdersFromStringBook`) and another that takes a numeric representation of the order book (`removeOwnOrdersFromNumericBook`). 3. The `n` function is used to parse string values as numbers, e.g., `n(amount)` converts a string `amount` to a number. 4. The `toQuantityString` function is used to convert a quantity (as a number) to a string representation in the format `<quantity>`. 5. The `fictStringOrderBook`, `fictNumericOrderBook`, `ownStringOrders`, and `ownNumericOrders` variables are placeholders for actual order book data. To make an educated guess about the optimal implementation, I'll consider some factors: 1. Performance: How do the two implementations compare in terms of execution time? 2. Readability: Which implementation is easier to understand and maintain? 3. Code complexity: Are there any potential performance bottlenecks or areas for optimization? Based on the provided code snippets, here are my observations: **`removeOwnOrdersFromStringBook`** * This implementation uses a `reduce` method, which can be a slow operation in JavaScript due to its iterative nature. * The use of `n` function to parse string values as numbers and then perform arithmetic operations might introduce additional overhead. **`removeOwnOrdersFromNumericBook`** * This implementation also uses a `reduce` method, but it's more concise and arguably easier to understand than the first one. * Since numeric values are being directly compared and added, there's potentially less overhead due to string parsing and formatting. Without actual benchmarking results or more detailed code analysis, I'll make an educated guess: **Optimal implementation:** `removeOwnOrdersFromNumericBook` However, without additional context, such as the specific use case or requirements, this answer remains speculative.
Related benchmarks:
_.sortBy vs native sort
dsdsdsdsdsdsds
Uniqe Items from Two array of Object
ISO 8601 parsing
Comments
Confirm delete:
Do you really want to delete benchmark?