Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Powers of two
(version: 0)
If they should be written in an array or in the loop as we go
Comparing performance of:
wiki vs me
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
wiki
var container = "MRKU576275"; var con = container.toUpperCase(); var sum = 0; for (i = 0; i < 10; i++) { var n = con.substr(i, 1); if (i < 4) { n = "0123456789A?BCDEFGHIJK?LMNOPQRSTU?VWXYZ".indexOf(con.substr(i, 1)); }; n *= Math.pow(2, i); sum += n; }; sum %= 11; sum %= 10; return sum
me
var container = "MRKU576275"; var con = container.toUpperCase(); var p = [1,2,4,8,16,32,64,128,256,512]; var s = 0; for (var n=0;n<10;n++) { if (n<4) { var c = con.charCodeAt(n)-55; s+=(Math.floor(c/10.2)+c)*p[n]; } else { s+=Number(con.charAt(n))*p[n]; }; }; var sum = s % 11 % 10; return sum
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
wiki
me
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'd be happy to help explain the provided JavaScript microbenchmark. **Overview** The benchmark measures the performance of two different approaches for calculating powers of 2 in a loop, which are then summed up and taken modulo 11 and 10. **Options Compared** There are two main options compared: 1. **Approach 1 (wiki)**: This approach uses a simple `if` statement to check if the character index is less than 4. If it is, it uses a lookup table (`"0123456789A?BCDEFGHIJK?LMNOPQRSTU?VWXYZ".indexOf(con.substr(i, 1))`) to find the corresponding power of 2 value. If not, it directly multiplies the character code with the corresponding power of 2. 2. **Approach 2 (me)**: This approach uses an array (`p`) to store powers of 2 values and an object (`con` created from a string) to iterate over characters in the input string. It uses `charCodeAt()` to get the ASCII code of each character, subtracts 55 (since 'A' has an ASCII code of 65), and then performs arithmetic operations on the result. **Pros and Cons** * **Approach 1 (wiki)**: + Pros: Simple and concise. + Cons: Uses a lookup table, which can be slower due to the indexing operation. Also, it only works for uppercase letters, while Approach 2 is more flexible. * **Approach 2 (me)**: + Pros: More flexible, as it handles both uppercase and lowercase letters correctly. It also avoids using a lookup table, reducing overhead. + Cons: More complex code, which can make maintenance harder. **Library Used** In the `me` approach, the `Number()` function is used, which converts a string to a number. In JavaScript, `Number()` takes two arguments: the value to convert and an optional radix (base). Since no radix is provided, it defaults to base 10. **Special JS Feature/Syntax** Neither of the approaches uses any special JavaScript features or syntax that requires deep knowledge of the language. **Other Alternatives** Other possible approaches could be: * Using a library like [FastMath](https://github.com/rgbcray/FastMath) for fast mathematical operations. * Using a different data structure, such as an object map (e.g., `const powersOfTwo = { 0: 1, 1: 2, ..., 9: 512 };`), to store and look up power of 2 values. Keep in mind that the best approach will depend on the specific use case and requirements.
Related benchmarks:
Powers of two
Powers of two
pow vs multiply v2
Math.pow(2,n) vs Table lookup vs bitwise
Math.pow vs multiplication with decimals and multiple integers
Comments
Confirm delete:
Do you really want to delete benchmark?