Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Verifica CPF
(version: 0)
Comparing performance of:
Sem recursividade vs Com recursividade
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Sem recursividade
var cpf = "12345678901"; function validateCPF(cpf) { cpf = cpf.replace(/\D/g, ''); if (cpf.length != 11) { return false; } if (Number(cpf) - Number(Array(12).join(cpf[0])) === 0) { return false; } var rcpf1 = cpf.substring(0, 9); var rcpf2 = cpf.substring(9); var d1 = 0; for (var i = 0; i < 9; i++) { d1 += parseInt(rcpf1.charAt(i)) * (10 - i); } d1 = 11 - (d1 % 11); if (d1 > 9) { d1 = 0; } if (parseInt(rcpf2.charAt(0)) != d1) { return false; } d1 *= 2; for (var j = 0; j < 9; j++) { d1 += parseInt(rcpf1.charAt(j)) * (11 - j); } d1 = 11 - (d1 % 11); if (d1 > 9) { d1 = 0; } if (parseInt(rcpf2.charAt(1)) != d1) { return false; } return true; } validateCPF(cpf);
Com recursividade
var cpf = "12345678901"; function validateCPF(cpf) { cpf = cpf.replace(/\D/g, ''); if (cpf.length != 11) { return false; } if (Number(cpf) - Number(Array(12).join(cpf[0])) === 0) { return false; } function verifyDigit(digit) { var length = 8 + digit; var sequence = 9 + digit; var sum = 0; for (var i = 0; i < length; i++) { sum += Number(cpf.charAt(i)) * (sequence - i); } var aux = sum % 11; if (cpf.charAt(length) != ((aux < 2) ? 0 : 11 - aux)) { return false; } return true; } return verifyDigit(1) && verifyDigit(2); } validateCPF(cpf);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Sem recursividade
Com 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):
Let's dive into the Benchmark Definition and Individual Test Cases. **Benchmark Definition** The provided JSON represents a benchmark definition, which is essentially a script that defines a test case or a benchmark to be executed. In this case, there are two benchmark definitions: 1. `Verifica CPF` (which means "Verify CPF" in Portuguese): This benchmark definition is empty, meaning it doesn't have any preparation code for the test. 2. The second benchmark definition is also empty. However, when we look at the Individual Test Cases, we see that there are two separate test cases defined: 1. `Sem recursividade` (meaning "Without recursion") 2. `Com recursividade` (meaning "With recursion") These two test cases have different implementation of the CPF validation function. **Individual Test Cases** Let's break down each test case: 1. **Sem recursividade** ```javascript function validateCPF(cpf) { cpf = cpf.replace(/\\D/g, ''); if (cpf.length != 11) { return false; } // ... (rest of the implementation) } ``` This test case uses a simple iterative approach to calculate the CPF verification digits. 2. **Com recursividade** ```javascript function validateCPF(cpf) { cpf = cpf.replace(/\\D/g, ''); if (cpf.length != 11) { return false; } function verifyDigit(digit) { // ... (rest of the implementation) } return verifyDigit(1) && verifyDigit(2); } ``` This test case uses a recursive approach to calculate the CPF verification digits. **Options compared** The two test cases compare the performance of: * Iterative vs. Recursive approaches for calculating CPF verification digits. * Whether to use recursion or not in the implementation. **Pros and Cons** **Iterative Approach (Sem recursividade)** Pros: * Typically faster than recursive approaches due to fewer function calls. * Easier to optimize and parallelize. Cons: * Can be more prone to stack overflow errors for large inputs. * May require more memory allocation. **Recursive Approach (Com recursividade)** Pros: * Can be more elegant and concise in implementation. * Can be easier to understand for some developers. Cons: * Typically slower than iterative approaches due to function call overhead. * More prone to stack overflow errors for large inputs. * May require more memory allocation. **Library usage** There is no explicit library used in the provided benchmark definitions. However, it's possible that the developer has included a `regex` library (e.g., `RegExp`) to implement the regular expression `\\D` for removing non-numeric characters from the CPF string. **Special JS feature/syntax** The benchmark definition uses the `replace` method and regular expressions to manipulate strings. There are no specific JavaScript features or syntax used in this benchmark that would require special handling. **Other alternatives** There are other approaches to calculating CPF verification digits, such as using a lookup table or a polynomial equation. However, these approaches may not be as straightforward to implement and understand as the iterative and recursive approaches shown here. It's worth noting that the benchmark definition can also include additional options, such as: * Different input data sets (e.g., different CPF numbers) * Additional test cases with varying complexity * Different performance metrics (e.g., throughput vs. latency) By experimenting with these alternatives, developers can gain a deeper understanding of their code's performance and optimization opportunities.
Related benchmarks:
fib test 1111
Factorials-comp
Factorials-comp
fibo-algo
Filter Cards
Comments
Confirm delete:
Do you really want to delete benchmark?