Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Verifica CNPJ
(version: 0)
Comparing performance of:
Recursividade vs Sem recursividade
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Recursividade
cnpj = "45586424000130"; function validateCNPJ(cnpj) { cnpj = cnpj.replace(/\D/g, ''); if (cnpj.length != 14) { return false; } if (Number(cnpj) - Number(Array(15).join(cnpj[0])) === 0) { return false; } function verifyDigit(digit) { var length = 11 + digit; var sequence = 4 + digit; var sum = 0; for (var i = 0; i < length; i++) { sum += cnpj.charAt(i) * sequence--; if (sequence < 2) { sequence = 9; } } var aux = sum % 11; if (cnpj.charAt(length) != (aux < 2 ? 0 : 11 - aux)) { return false; } return true; } return verifyDigit(1) && verifyDigit(2); } validateCNPJ(cnpj);
Sem recursividade
cnpj = "12345678901234"; function validateCNPJ(cnpj) { cnpj = cnpj.replace(/\D/g, ''); if (cnpj.length != 14) { return false; } if (Number(cnpj) - Number(Array(15).join(cnpj[0])) === 0) { return false; } var length = 12; var sequence = 5; var sum = 0; for (var i = 0; i < length; i++) { sum += cnpj.charAt(i) * sequence--; if (sequence < 2) { sequence = 9; } } var aux = sum % 11; if (cnpj.charAt(length) != (aux < 2 ? 0 : 11 - aux)) { return false; } length = 13; sequence = 6; sum = 0; for (var i = 0; i < length; i++) { sum += cnpj.charAt(i) * sequence--; if (sequence < 2) { sequence = 9; } } aux = sum % 11; if (cnpj.charAt(length) != (aux < 2 ? 0 : 11 - aux)) { return false; } return verifyDigit(1) && !verifyDigit(2); } console.log(validateCNPJ(cnpj));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Recursividade
Sem recursividade
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):
I'll break down the benchmark definition, test cases, and results to explain what's being tested. **Benchmark Definition** The benchmark definition is not provided as a JSON object, but rather as a script that defines two functions: `validateCNPJ(cnpj)` with recursion and another one without recursion. The script also includes some helper function `verifyDigit(digit)`. The main difference between the two functions is the presence of recursive calls. **Options Being Compared** The benchmark compares two approaches: 1. **Recursive approach**: This implementation uses a recursive function to calculate the digits of the CNPJ (Brazilian corporate identification number). The recursive function calls itself for each digit, which can lead to performance issues due to the repeated calculations. 2. **Non-recursive approach**: This implementation uses a loop to calculate the digits of the CNPJ, avoiding the recursive calls. **Pros and Cons** * Recursive approach: + Pros: Easier to understand and implement, especially for small numbers of digits. + Cons: May lead to performance issues due to repeated calculations, potential stack overflow errors, and slower execution times. * Non-recursive approach: + Pros: Faster execution times, reduced memory usage, and less chance of stack overflow errors. + Cons: More complex implementation, requiring careful handling of the loop and digit calculation. **Library Used** There is no explicit library mentioned in the benchmark definition. However, it's likely that the `Array` function and the `charAt()` method are used from the built-in JavaScript arrays or string objects. **Special JS Feature/Syntax** There is a special syntax feature being tested: **async/await**. In the latest benchmark result, there is no mention of async/await in the results, but this is likely due to the test framework's configuration or the execution environment not supporting it. **Other Considerations** The benchmark is designed to measure the performance difference between recursive and non-recursive approaches. The use of a large input value (`cnpj`) helps to highlight any performance differences. **Alternatives** If you're interested in exploring alternative approaches, here are a few: * **Iterative approach**: Instead of using loops or recursion, consider using mathematical formulas to calculate the digits directly. * **Parallelization**: If possible, use parallel processing techniques to take advantage of multiple CPU cores and improve performance. * **Just-In-Time (JIT) compilation**: Consider using JIT compilers like V8 in Chrome or SpiderMonkey in Firefox to optimize the code for better performance. Keep in mind that these alternatives might not directly address the specific issue being tested but can provide additional insights into optimization techniques.
Related benchmarks:
unique-props
custom_deepEqual vs. lodash.isEqual
schema vs manual
schema vs manual f
Object.keys for length checking
Comments
Confirm delete:
Do you really want to delete benchmark?