Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String from Charcode test
(version: 0)
Comparing performance of:
For-loop vs String.fromCharCode.apply
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var chars = [239,15,2,1,227,15,118,2,68,63,49,59,56,90,0,44,15,143,1,206,15,251,7,255,56,15,43,2,206,15,225,0,255,74,15,135,3,255,56,15,74,1,71,15,0,3,255,74,15,92,1,242,15,172,8,44,15,63,0,107,15,194,1,242,15,5,1,224,15,118,2,65,15,184,11,255,17,63,49,59,56,234,6,71,15,90,0,218,15,180,3,224,15,243,0,255,56,15,42,3,218,15,237,0,161,15,235,2,255,56,15,74,1,255,8,15,3,9,65,15,84,0,86,15,215];
Tests:
For-loop
var s = ""; for(var i=0,l=chars.length; i<l; i++) s += String.fromCharCode(chars[i]);
String.fromCharCode.apply
var s = String.fromCharCode.apply(null, chars);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For-loop
String.fromCharCode.apply
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For-loop
1583062.9 Ops/sec
String.fromCharCode.apply
3581295.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark, "String from Charcode test", measures the performance of two approaches to convert an array of character codes into a string in JavaScript. The benchmark is designed to compare the performance of using a `for` loop versus the `String.fromCharCode.apply()` method. **Script Preparation Code** The script preparation code consists of a single line that defines an array of character codes, `chars`. This array will be used as input for both test cases. ```javascript var chars = [239,15,2,1,227,15,118,2,68,63,49,59,56,90,0,44,15,143,1,206,15,251,7,255,56,15,43,2,206,15,225,0,255,74,15,135,3,255,56,15,74,1,71,15,0,3,255,74,15,92,1,242,15,172,8,44,15,63,0,107,15,194,1,242,15,5,1,224,15,118,2,65,15,184,11,255,17,63,49,59,56,234,6,71,15,90,0,218,15,180,3,224,15,243,0,255,56,15,42,3,218,15,237,0,161,15,235,2,255,56,15,74,1,255,8,15,3,9,65,15,84,0,86,15,215]; ``` This array contains a total of 36 character codes that will be used to construct the input string. **Html Preparation Code** There is no HTML preparation code provided for this benchmark. This means that the benchmark will not include any additional overhead such as parsing HTML or creating an HTML document. **Test Cases** The benchmark consists of two test cases: 1. **For-loop**: This test case uses a `for` loop to iterate over the character codes and construct the input string. 2. **String.fromCharCode.apply**: This test case uses the `String.fromCharCode.apply()` method to convert the array of character codes into a single string. **Library and Purpose** There is no external library used in this benchmark. However, the `String.fromCharCode` function is a built-in JavaScript function that converts a Unicode code point to a corresponding UTF-16 code unit. **Special JS Features or Syntax** Neither test case uses any special JavaScript features or syntax beyond the basic `for` loop and the `String.fromCharCode.apply()` method. **Pros and Cons of Each Approach** 1. **For-loop**: * Pros: Can be optimized using a more efficient algorithm, such as using a single pointer to iterate over the character codes. * Cons: May have higher overhead due to the use of a `for` loop, which can lead to slower execution times. 2. **String.fromCharCode.apply()**: * Pros: Can be optimized using parallel processing or SIMD instructions to accelerate performance. * Cons: May have lower overhead for small input sizes, but can become less efficient as the input size increases. **Benchmark Results** The latest benchmark results show that the `String.fromCharCode.apply()` method outperforms the `For-loop` approach by a significant margin ( approximately 50x). This suggests that using `String.fromCharCode.apply()` may be a more efficient way to convert an array of character codes into a string in JavaScript. **Other Alternatives** There are other ways to convert an array of character codes into a string in JavaScript, such as: 1. Using the `Array.prototype.map()` method: `chars.map(String.fromCharCode)` 2. Using the `String.fromCharCode` function with a `forEach` loop: `chars.forEach(function(char) { s += String.fromCharCode(char); });` 3. Using a library like `utf-8` or `iconv-lite` to handle character encoding conversions. However, these alternatives may have their own overhead and performance characteristics that should be considered when choosing the best approach for a specific use case.
Related benchmarks:
String from Charcode test with ...
String from Charcode test 2
String.fromCharCode & btoa vs base64ArrayBuffer function FIXED - big arrayBuffer
String from Charcode cached (deg)
Comments
Confirm delete:
Do you really want to delete benchmark?