Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Hex To RGB
(version: 0)
Comparing performance of:
Test 1 vs Test 2 with regex
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const colorStr = `#617448 #9158f7 #32b693 #ec4a5e #c788ae #b10142 #4459bd #b8c378 #09a0ed #34526b #a9e4fc #49292e #b5be83 #938790 #acefe4 #3e84fd #cc2bd9 #af0039 #c8c98b #7cc282 #9a417a #602fe7 #ab0634 #11a4c2 #3b1068 #cf09f1 #771dd3 #d41cd0 #1c1b69 #b6a902 #3c97e6 #5af788 #4e91e8 #bc8d13 #44d787 #9bbe49 #af79d8 #b4089c #7c025d #b53d8d #aef27f #c6f77e #2b916a #3f327f #755946 #487797 #f4d94a #8dcca9 #e6be20 #8a050d #0e4659 #d03eef #bcc370 #3627fc #e9b512 #cec1a2 #cb025a #9fa730 #039331 #18cccd #914b68 #96a38a #4fc0ad #4dd18d #87ec3e #500076 #ca825a #dd95be #758b5b #703699 #e9c201 #e6cc0f #0615a3 #5bf287 #544441 #69fce2 #c51c06 #3f05bb #92adf8 #f9802d #beeb7b #f308dd #8e22f6 #36146b #923250 #258568 #8445b4 #73813a #cc1006 #1c53b0 #fff1f4 #7fa7cc #014c54 #49a646 #1a31d0 #3871f8 #03dd31 #1a3193 #0298b0 #2a7ba8` var colors = colorStr.split('\n')
Tests:
Test 1
function hexToRgbNew(hex) { const bigint = parseInt(hex.replace('#', ''), 16); const r = (bigint >> 16) & 255; const g = (bigint >> 8) & 255; const b = bigint & 255; return [r, g, b]; } colors.forEach((color)=> hexToRgbNew(color))
Test 2 with regex
function hexToRgb(hex) { // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; hex = hex.replace(shorthandRegex, function(m, r, g, b) { return r + r + g + g + b + b; }); var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); return [ parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16), ] } colors.forEach((color)=> hexToRgb(color))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Test 1
Test 2 with regex
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 provided benchmark definition and options for you. **Benchmark Definition:** The benchmark measures the performance of two JavaScript functions, `hexToRgbNew` and `hexToRgb`, which convert hexadecimal color codes to RGB values. The benchmark runs these functions on a list of 36 predefined colors (`colors`) using a `forEach` loop. **Options Compared:** There are two options compared: 1. **`hexToRgbNew`**: This function uses bitwise operations to extract the red, green, and blue components from the hexadecimal color code. 2. **`hexToRgb`**: This function uses regular expressions to expand shorthand hexadecimal color codes (e.g., `#03F`) to full form (e.g., `0033FF`). **Pros and Cons:** * **`hexToRgbNew`**: + Pros: - Faster execution, as it avoids the overhead of regular expression matching. + Cons: - May not work correctly for shorthand hexadecimal color codes (e.g., `#03F`). * **`hexToRgb`**: + Pros: - Works correctly for shorthand hexadecimal color codes. + Cons: - Slower execution, due to the regular expression matching. **Library and Purpose:** None of the benchmark functions rely on external libraries. However, `colorStr` is a string containing multiple hexadecimal color codes, which is split into an array using the `\n` character as a delimiter. **Special JS Feature or Syntax:** The benchmark uses bitwise operations (`>>` and `&`) to extract the red, green, and blue components from the hexadecimal color code. This is a native JavaScript feature that allows for efficient bit-level manipulation. **Other Considerations:** * The benchmark measures the performance of both functions on a small dataset (36 colors). A larger dataset or more complex input may affect the results. * The `forEach` loop is used to iterate over the array of colors. This can be optimized using other iteration methods, such as `map()` or `reduce()`, if necessary. **Alternatives:** Other alternatives for measuring the performance of color conversion functions might include: 1. Using a library like `color-convert` or `hex-to-rgba` to perform the conversions. 2. Creating a custom implementation using a different algorithm or data structure. 3. Using a testing framework that supports benchmarking, such as Jest or Mocha. Keep in mind that these alternatives may not be directly comparable to the provided benchmark, and their performance characteristics may vary depending on the specific requirements and constraints of your use case.
Related benchmarks:
parse hex to bytes
Add alpha color
IntToRGBA
array buffer to hex conversion
Comments
Confirm delete:
Do you really want to delete benchmark?