Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor] Use indexed access to string chars #20

Closed
wants to merge 1 commit into from

Conversation

victor-homyakov
Copy link

There is no need to split() string into array of chars: string also has indexed access

@victor-homyakov
Copy link
Author

One of Travis CI jobs (test in browser) fails because of error in zuul: SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

https://travis-ci.org/github/unshiftio/yeast/jobs/663048501#L321

Might be fixed in #18

@lpinca
Copy link
Member

lpinca commented Mar 18, 2020

Using an array seems to be faster.

const Benchmark = require('benchmark');

const suite = new Benchmark.Suite();

const string =
  '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_';
const array = string.split('');

let result;

function randomIndex() {
  return Math.floor(Math.random() * 64);
}

suite.add('string', function() {
  result = string[randomIndex()];
  return result;
});

suite.add('array', function() {
  result = array[randomIndex()];
  return result;
});

suite.on('cycle', function(event) {
  console.log(event.target.toString());
});

suite.on('complete', function() {
  console.log('Fastest is ' + this.filter('fastest').map('name'));
});

suite.run({ async: true });
$ node index.js 
string x 69,631,394 ops/sec ±0.34% (93 runs sampled)
array x 80,212,620 ops/sec ±0.30% (91 runs sampled)
Fastest is array

@victor-homyakov
Copy link
Author

Very interesting difference in performance. What about other JS engines?

@lpinca
Copy link
Member

lpinca commented Mar 20, 2020

I don't know I've only run it on Node.js 13.x

@victor-homyakov
Copy link
Author

I've added two more checks to see whether speed differences are systematic or random

const Benchmark = require('benchmark');
const suite = new Benchmark.Suite();
const string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_';
const array = string.split('');

let result;

function randomIndex() {
  return Math.floor(Math.random() * 64);
}

suite.add('string 1', function() {
  return string[randomIndex()];
});

suite.add('array 1', function() {
  return array[randomIndex()];
});

suite.add('string 2', function() {
  return string[randomIndex()];
});

suite.add('array 2', function() {
  return array[randomIndex()];
});

suite.on('cycle', function(event) {
  console.log(event.target.toString());
});

suite.on('complete', function() {
  console.log('Fastest is ' + this.filter('fastest').map('name'));
});

suite.run({ async: true });

Two benchmark runs in Node 8.6.0:

string 1 x 49,305,524 ops/sec ±1.02% (85 runs sampled)
array 1 x 48,975,469 ops/sec ±0.55% (90 runs sampled)
string 2 x 49,163,275 ops/sec ±1.44% (90 runs sampled)
array 2 x 48,691,671 ops/sec ±0.65% (85 runs sampled)
Fastest is string 1,string 2

string 1 x 48,392,746 ops/sec ±1.52% (87 runs sampled)
array 1 x 49,001,437 ops/sec ±0.45% (95 runs sampled)
string 2 x 50,636,402 ops/sec ±0.56% (85 runs sampled)
array 2 x 49,098,526 ops/sec ±0.97% (86 runs sampled)
Fastest is string 2

Two benchmark runs in Node 12.13.0:

string 1 x 64,548,401 ops/sec ±0.81% (92 runs sampled)
array 1 x 77,401,569 ops/sec ±0.45% (92 runs sampled)
string 2 x 63,871,694 ops/sec ±2.70% (85 runs sampled)
array 2 x 76,654,940 ops/sec ±1.11% (89 runs sampled)
Fastest is array 1,array 2

string 1 x 63,000,088 ops/sec ±1.60% (87 runs sampled)
array 1 x 76,415,302 ops/sec ±0.51% (95 runs sampled)
string 2 x 63,639,327 ops/sec ±2.86% (87 runs sampled)
array 2 x 76,791,568 ops/sec ±1.27% (92 runs sampled)
Fastest is array 1

The same benchmark for browsers: https://jsperf.com/random-array-access-vs-random-string-access/1

Ops/sec in different browsers:

Data Chrome 80 Chrome 83 Firefox 74 Safari 13
array 68,543,368 68,946,341 221,238,676 10,146,724
string 63,945,222 66,640,438 221,776,737 10,273,448

While Firefox and Safari show similar speed for both array and string, fresh Node.js versions and Chrome browsers obviously prefer the array as the fastest structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants