Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_last vs regexdfdsfsfsdf
(version: 0)
Comparing performance of:
_last vs regex
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.21/lodash.min.js"></script>
Tests:
_last
const url = 'https://bo.test.danim.com/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/aeda0cf9-4f25-4d93-ba14-f8d5635a2817'; const id = _.last(url.split('/')); console.log(id);
regex
const url = 'https://bo.test.danim.com/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/template/list/item/aeda0cf9-4f25-4d93-ba14-f8d5635a2817'; const regex = /\/item\/([a-z0-9-]+)/; const match = regex.exec(url); const id = match[1]; console.log(id);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_last
regex
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
gemma2:9b
, generated one year ago):
This benchmark tests two different ways to extract the last part of a URL string. **Method 1: Using Lodash's `_.last()`** * **Description:** This method utilizes the `_.last()` function from the Lodash library, which efficiently retrieves the last element of an array. The URL is split into an array using `/` as the delimiter, and then `_.last()` is applied to obtain the final segment. * **Pros:** Concise, readable, and leverages a well-established library for common array operations. * **Cons:** Relies on an external library (Lodash). **Method 2: Using Regular Expressions** * **Description:** This method defines a regular expression (`/\\/item\\/([a-z0-9-]+)/`) that captures the last part of the URL after `/item/`. The `exec()` function then finds the first match in the URL string, and the captured group (match[1]) is extracted as the result. * **Pros:** More flexible for complex pattern matching; can be used in various scenarios beyond simple URL extraction. * **Cons:** Can be more verbose and harder to read compared to _.last() when the task is straightforward. **Alternatives:** * **Native JavaScript Array Methods:** You could use `url.split('/').pop()` to achieve the same result as `_.last()` without relying on Lodash. This approach is generally less readable than `_.last()` but avoids external dependencies. * **Built-in URL Parsing:** If you need more advanced URL manipulation, consider using JavaScript's built-in `URL` object to parse and extract components like the path, query parameters, etc. Let me know if you have any other questions!
Related benchmarks:
Find and Tail (Vanilla JS vs Lodash)
Last Lodash Test
_last vs regex
Random perf
Comments
Confirm delete:
Do you really want to delete benchmark?