mc_convertingString2Array

Benchmark created on


Description

We test different algorithms for converting the string to the array of chars.

Setup

const str = "The facts of the case are simple. Sir Charles Baskerville was in the habit every night before going to bed of walking down the famous Yew Alley of Baskerville Hall. The evidence of the Barrymores shows that this had been his custom. On the 4th of May Sir Charles had declared his intention of starting next day for London, and had ordered Barrymore to prepare his luggage. That night he went out as usual for his nocturnal walk, in the course of which he was in the habit of smoking a cigar. He never returned. At twelve o'clock Barrymore, finding the hall door still open, became alarmed, and, lighting a lantern, went in search of his master. The day had been wet, and Sir Charles's footmarks were easily traced down the Alley. Half-way down this walk there is a gate which leads out on to the moor. There were indications that Sir Charles had stood for some little time here. He then proceeded down the Alley, and it was at the far end of it that his body was discovered. One fact which has not been explained is the statement of Barrymore that his master's footprints altered their character from the time that he passed the moor-gate, and that he appeared from thence onward to have been walking upon his toes. One murphy, a gipsy horse-dealer, was on the moor at no great distance at the time, but he appears by his own confession to have been the worse for drink. He declares that he heard cries, but is unable to state from what direction they came. No signs of violence were to be discovered upon Sir Charles's person, and though the doctor's evidence pointed to an almost incredible facial distortion--so great that Dr. mortimer refused at first to believe that it was indeed his friend and patient who lay before him--it was explained that that is a symptom which is not unusual in cases of dyspnoea and death from cardiac exhaustion. This explanation was borne out by the post-mortem examination, which showed long-standing organic disease, and the coroner's jury returned a verdict in accordance with the medical evidence. It is well that this is so, for it is obviously of the utmost importance that Sir Charles's heir should settle at the Hall and continue the good work which has been so sadly interrupted. Had the prosaic finding of the coroner not finally put an end to the romantic stories which have been whispered in connection with the affair, it might have been difficult to find a tenant for Baskerville Hall. It is understood that the next of kin is mr. Henry Baskerville, if he be still alive, the son of Sir Charles Baskerville's younger brother. The young man when last heard of was in America, and inquiries are being instituted with a view to informing him of his good fortune.";

Test runner

Ready to run.

Testing in
TestOps/sec
.split()
const arr = str.split('');
ready
Array.from()
const arr = Array.from(str);
ready
... Spread operator
const arr = [...str];
ready
[] + for + .push()
const arr = [];
for (let i = 0; i < str.length; i++) {
  arr.push(str[i]);
}
ready
array + for
const arr = new Array(str.length).fill();
for (let i = 0; i < str.length; i++) {
  arr[i] = str[i];
}
ready
[] + for in + .push()
const arr = [];
for (const char of str) {
  arr.push(char);
}
ready
.match() + RegExp
const arr = str.match(/./gu);
ready
.match() + RegExp #2
const arr = str.match(/./g);
ready
[] + while + .push()
const arr = [];
let i = 0;
while (i < str.length) {
  arr.push(str[i]);
  i++;
}
ready
array + while
const arr = new Array(str.length).fill();
let i = 0;
while (i < str.length) {
  arr[i] = str[i];
  i++;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.