<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var text = "1KX5 P/CKD TORTELLINI BRAIS BEEF CA CT 631 ***** note: we don't sell this. *****";
text.indexOf('note') !== 1
text.includes('note')
_.includes(text, 'note')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
IndexOf | |
Includes | |
lodash |
Test name | Executions per second |
---|---|
IndexOf | 1562450816.0 Ops/sec |
Includes | 1558164736.0 Ops/sec |
lodash | 35513388.0 Ops/sec |
Measuring that net is an excellent resource for comparing the performance of different JavaScript methods.
Benchmark Definition
The provided JSON represents a benchmark test for finding a substring in a string using three different methods: indexOf
, includes
, and a custom implementation with Lodash's includes
function. The test checks which method is faster, providing insights into the performance characteristics of each approach.
Options Compared
indexOf
: This method searches for the specified value in the string and returns the index of the first occurrence. If the value is not found, it returns -1.includes
: This method checks if a string includes another string as a substring. It's similar to indexOf
, but it doesn't return an index; instead, it returns true
or false
.lodash.includes
: This function is part of the Lodash library and performs the same check as includes
. However, its implementation might be optimized for performance.Pros and Cons
indexOf
: Pros: Simple to implement, widely supported. Cons: Returns an index (if found) or -1 if not found, which can lead to unnecessary calculations.includes
: Pros: Similar to indexOf
, but returns a boolean value, making it more concise. Cons: Also returns true
or false
, similar to indexOf
.lodash.includes
: Pros: Optimized for performance, provides a simple and concise API. Cons: Requires the Lodash library, which might add overhead.Library Usage
In this benchmark, the Lodash library is used for its includes
function. This suggests that the author wants to compare the performance of the built-in JavaScript methods with a popular utility function from another library.
Special JS Feature/Syntax
There doesn't appear to be any special JavaScript features or syntax mentioned in the provided code snippets. The focus is on comparing different string search methods.
Other Alternatives
If you're interested in exploring alternative approaches, here are some other options:
String.prototype.includes()
: Similar to includes
, but part of the standard JavaScript API.Array.prototype.indexOf()
: If you want to find an index within a larger array or string.For further information on measuring performance in JavaScript, consider checking out Measuring that net and other resources.