Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Truncate performance
(version: 0)
Description
Comparing performance of:
lodash vs Custom
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Tests:
lodash
const text = `Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer malesuada. Fusce suscipit libero eget elit. Cras elementum. Pellentesque pretium lectus id turpis. Curabitur sagittis hendrerit ante. Etiam dictum tincidunt diam. Phasellus rhoncus. Nulla non lectus sed nisl molestie malesuada. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Nulla accumsan, elit sit amet varius semper, nulla mauris mollis quam, tempor suscipit diam nulla vel leo. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Sed elit dui, pellentesque a, faucibus vel, interdum nec, diam. Fusce tellus odio, dapibus id fermentum quis, suscipit id erat. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Maecenas fermentum, sem in pharetra pellentesque, velit turpis volutpat ante, in pharetra metus odio a lectus. Curabitur vitae diam non enim vestibulum interdum. In convallis. Duis pulvinar. Maecenas lorem. Aliquam erat volutpat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Integer lacinia. Aenean fermentum risus id tortor. Fusce susci`; console.log(_.truncate(text, {length: 250, separator: ' '}));
Custom
const text = `Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer malesuada. Fusce suscipit libero eget elit. Cras elementum. Pellentesque pretium lectus id turpis. Curabitur sagittis hendrerit ante. Etiam dictum tincidunt diam. Phasellus rhoncus. Nulla non lectus sed nisl molestie malesuada. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Nulla accumsan, elit sit amet varius semper, nulla mauris mollis quam, tempor suscipit diam nulla vel leo. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Sed elit dui, pellentesque a, faucibus vel, interdum nec, diam. Fusce tellus odio, dapibus id fermentum quis, suscipit id erat. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Maecenas fermentum, sem in pharetra pellentesque, velit turpis volutpat ante, in pharetra metus odio a lectus. Curabitur vitae diam non enim vestibulum interdum. In convallis. Duis pulvinar. Maecenas lorem. Aliquam erat volutpat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Integer lacinia. Aenean fermentum risus id tortor. Fusce susci`; const indexOfSpace = text.indexOf(' ', length); const spacePos = (indexOfSpace === -1 ? text.length : indexOfSpace) + 1; let substr = text.substr(0, ((spacePos > length && spacePos < (length + 10)) ? spacePos : length)).trim(); if(substr.substr(substr.length - 3) !== '...') { substr += '...'; } console.log(substr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
Custom
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
317153.6 Ops/sec
Custom
319802.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Definition** The benchmark definition is a JSON object that describes the test. It has four properties: * `Name`: The name of the benchmark (in this case, "Truncate performance"). * `Description`: A brief description of the benchmark. * `Script Preparation Code`: An optional code snippet to prepare the environment before running the test. In this case, it's empty. * `Html Preparation Code`: A script tag that loads the Lodash library, which is used in one of the test cases. **Individual Test Cases** There are two test cases: 1. **`lodash`**: This test uses the `lodash` library to truncate a long string. The `truncate` function takes three arguments: the input string, the desired length, and an optional separator. 2. **`Custom`**: This test is a custom implementation that truncates a long string using a simple algorithm. It finds the last space character within a certain range (10 characters before or after the desired length) and trims the string to that point. **Options Compared** The two test cases are comparing different approaches to truncate strings: * **`lodash`**: Uses the `truncate` function from Lodash, which is likely optimized for performance. * **`Custom`**: Implements a custom truncation algorithm using JavaScript's built-in string manipulation functions (`indexOf`, `substr`, and `trim`). **Pros and Cons of Each Approach** 1. **`lodash`**: * Pros: + Optimized for performance by the Lodash library. + Less error-prone due to the library's implementation. * Cons: + Adds an external dependency (Lodash) that may not be desirable in all scenarios. + May introduce additional overhead due to the library's initialization and garbage collection. 2. **`Custom`**: * Pros: + No external dependencies or overhead. + Can be optimized for specific use cases or performance requirements. * Cons: + More error-prone due to the custom implementation. + May have slower performance compared to the optimized `lodash` library. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions, including `truncate`. The `truncate` function takes two arguments (input string and desired length) and returns a new string with the input truncated to the specified length. Lodash's implementation is likely optimized for performance and includes features like caching and memoization. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax mentioned in this benchmark. It only uses standard JavaScript functions and libraries (Lodash). **Other Alternatives** If you want to implement a custom string truncation algorithm without using Lodash, you could consider other libraries or frameworks that provide similar functionality, such as: * `String.prototype.slice()` * `String.prototype.substr()` * `String.prototype.substring()` Alternatively, you could implement your own algorithm using JavaScript's built-in functions and data structures.
Related benchmarks:
Ramda vs. Lodash vs Fastest Clone
Comparing performance of native .length and Lodash _.isEmpty
Ramda vs. Lodash vs Natice (filter, find)
Map (Native vs Ramda vs Lodash vs Immutable) with lambda function
Random perf
Comments
Confirm delete:
Do you really want to delete benchmark?