sumDigits

Benchmark created on


Setup

const sumDigitsInt = int => {
	let sum = 0;
	while (int) {
		sum += int % 10;
		int = Math.floor(int / 10);
	}

	return sum;
};

const sumDigitsStr = str => {
	let sum = 0;
	for (s of str) {
		sum += Number.parseInt(s, 10);
	}
	
	return sum;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Number
sumDigitsInt(123985723);
ready
String; parseInt
sumDigitsStr('123985723')
ready

Revisions

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