Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String isNullOrWhitespace
(version: 0)
Test is fastest way to detect isNullOrWhitespace
Comparing performance of:
Trim vs RegExp
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const strings = []; for (let i = 0; i < 1000; ++i) { const string = i % 2 === 0 ? ' ' : (Math.random() * 100000).toFixed(0).toString() strings.push(string); } window.strings = strings;
Tests:
Trim
function isNullOrWhitespace(str) { return !str || !str.trim(); } window.strings.forEach(string => isNullOrWhitespace(string));
RegExp
const regExp = /^ *$/; function isNullOrWhitespace(str) { return !str || regExp.test(str); } window.strings.forEach(string => isNullOrWhitespace(string));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Trim
RegExp
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Trim
93113.4 Ops/sec
RegExp
45577.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided benchmark tests two approaches to detect whether a string is null or whitespace: using the `trim()` method and regular expressions (`RegExp`). **Options compared:** 1. **Trim() method**: This approach uses the `trim()` method, which removes leading and trailing whitespace from a string. 2. **RegExp**: This approach uses a regular expression `/^ *$/` to match strings that consist only of whitespace characters. **Pros and Cons of each approach:** **Trim() method:** Pros: * Simple and intuitive * Works well for most cases Cons: * May not be as efficient as RegExp, especially for large strings * Can be slower if the string contains non-ASCII characters **RegExp:** Pros: * More efficient than Trim() for large strings * Can handle non-ASCII characters more easily Cons: * May require more setup and complexity (the regular expression `/^ *$/` needs to be defined) * Less intuitive for some developers **Other considerations:** * The benchmark uses a mix of null, whitespace-only strings, and random strings to simulate real-world usage. * The `window.strings` array is populated with 1000 strings before running the test, which may affect the results. * The `isNullOrWhitespace()` function is called on each string in the array, which means that the function itself is included in the benchmark. **Libraries and special JS features:** There are no libraries used in this benchmark. However, it's worth noting that some browsers may have specific optimizations or quirks when using regular expressions for string comparison. **Other alternatives:** If you wanted to test other approaches to detect null or whitespace strings, you could consider: * Using a more advanced RegExp pattern (e.g., `/^\s*$/i` to match strings with only whitespace characters and ignore case) * Implementing a custom solution using JavaScript's built-in `String.prototype === undefined || String.prototype.trim() === ''` Keep in mind that these alternatives may not be relevant for this specific benchmark, which is focused on comparing the performance of two simple approaches.
Related benchmarks:
whitespace detection 2
String typeof in High Frequency
Skip Substr if length is less
Object.create(null) vs {} vs Map() key access (heavy)
Comments
Confirm delete:
Do you really want to delete benchmark?