Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Valid Braces 01
(version: 1)
Comparing performance of:
MapSet vs HardCore
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var noop = Function.prototype; var isValidByMapSet = function (s) { let map = new Map(); map.set('{', '}'); map.set('(', ')'); map.set('[', ']'); let b = []; for (let i = 0; i < s.length; i++) { if (map.has(s.charAt(i))) { b.push(s.charAt(i)); } else { let pop = b.pop(); if (map.get(pop) !== s.charAt(i)) { return false; } } } return b.length === 0; }; var isValidByHardCore = (function() { let level; let index; const array = new Array(1e4); // const CLOSED_SQUARE = 93; // ']'.charCodeAt(0); // const CLOSED_CURLY = 125; // '}'.charCodeAt(0); // const CLOSED_PARENTHESIS = 41; // ')'.charCodeAt(0); // const OPEN_SQUARE = 91; // '['.charCodeAt(0); // const OPEN_CURLY = 123; // '{'.charCodeAt(0); // const OPEN_PARENTHESIS = 40; // '('.charCodeAt(0); // const ID_SQUARE = {}; // just uniq object // const ID_CURLY = {}; // just uniq object // const ID_PARENTHESIS = {}; // just uniq object return (s) => { level = -1; index = s.length; while (index--) switch (s.charCodeAt(index)) { case 93: array[++level] = 0; break; case 125: array[++level] = 1; break; case 41: array[++level] = 2; break; case 91: if (array[level--] !== 0) return false; break; case 123: if (array[level--] !== 1) return false; break; case 40: if (array[level--] !== 2) return false; break; } return level === -1; } })();
Tests:
MapSet
if (isValidByMapSet('{}[ adas]( asdasd asdd sd) ad{ ada da{ asda {dsasadssdadsa sadas dasdas [](){{{{czxc xzcxz cz}}}[()]}da asd adsd asd [[[[[[[[[3434]]]]]]]]]asdsa da dasd asd (((3333)))asdsadasd{{{}}} asd asd asd asd}}}') == true) noop(); if (isValidByMapSet('{}[ 1232 3123 312 12 213 12 123213 []{}{}{ asd asasd asda a}2d d adassasd dadsadasdas]()') == false) noop();
HardCore
if (isValidByHardCore('{}[ adas]( asdasd asdd sd) ad{ ada da{ asda {dsasadssdadsa sadas dasdas [](){{{{czxc xzcxz cz}}}[()]}da asd adsd asd [[[[[[[[[3434]]]]]]]]]asdsa da dasd asd (((3333)))asdsadasd{{{}}} asd asd asd asd}}}') == true) noop(); if (isValidByHardCore('{}[ 1232 3123 312 12 213 12 123213 []{}{}{ asd asasd asda a}2d d adassasd dadsadasdas]()') == false) noop();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
MapSet
HardCore
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):
The provided JSON represents a benchmark test case for JavaScript microbenchmarking on the MeasureThat.net website. **Benchmark Definition** The test case uses two different approaches to validate if a given string `s` is a valid braces sequence: 1. `isValidByMapSet`: This approach uses a `Map` data structure to store the matching pairs of opening and closing brackets, parentheses, and square brackets. It iterates through each character in the input string `s`, checking if it matches any of the keys in the map. If a match is found, the corresponding value is pushed onto an array `b`. After iterating through all characters, it checks if the length of `b` is zero; if so, the string is considered valid. 2. `isValidByHardCore`: This approach uses a more complex algorithm that utilizes an array to keep track of the nesting level and matching pairs of brackets. It iterates through each character in the input string `s`, updating the nesting level and checking for matches against predefined constants representing opening and closing brackets. **Comparison** The test case compares the performance of these two approaches: * `isValidByMapSet` * `isValidByHardCore` **Pros and Cons** 1. `isValidByMapSet`: * Pros: Easy to understand, uses a simple data structure (Map), and has a linear time complexity. * Cons: May be slower due to the overhead of creating and iterating through the map. 2. `isValidByHardCore`: * Pros: Can be faster due to optimized algorithms and reduced memory allocation, but its implementation is more complex. * Cons: Harder to understand, uses a more complex algorithm with nested loops and array operations. **Library and Special Features** There are no libraries used in this benchmark definition. However, the `noop()` function is used as a placeholder for a no-operation function, which is not explicitly mentioned in the benchmark definition but is likely included by MeasureThat.net to measure any overhead due to its presence. The special features being tested here are: * JavaScript's built-in data structures (Map and Array) * Optimized algorithms for string validation * Performance differences between two approaches **Alternatives** Other alternatives for implementing string validation in JavaScript could include using regular expressions, a recursive function approach, or other optimization techniques. However, these options would likely have different performance characteristics compared to the `isValidByMapSet` and `isValidByHardCore` approaches. In summary, the benchmark test case compares two distinct approaches to validating string braces sequences: one using a simple Map data structure (`isValidByMapSet`) and another employing a more complex algorithm with array operations (`isValidByHardCore`). The choice of approach depends on the desired balance between performance, simplicity, and readability.
Related benchmarks:
isIteratorOrAsyncIterator
double equals null vs boolean
typeof boolean vs === true || === false
empty test
Test AAAAA
Comments
Confirm delete:
Do you really want to delete benchmark?