Iteraring over valus using for loop of for-of (v5)

Revision 5 of this benchmark created on


Setup

function random(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

const columnCount = 100;
const rowCount = 10000;

const columnNames = new Array(columnCount).fill().map(() => `Column-${random(0, rowCount*rowCount)}`)
const columnTypes = new Array(columnCount).fill(`integer`)

var data = new Array(rowCount);
for (let i = 0; i < rowCount; i++) {
  data[i] = new Array(columnCount).fill(random(0, rowCount*rowCount))
}

const res = columnNames.reduce((acc, columnName) => {
      acc[columnName] = { maximum: Number.MIN_VALUE, minimum: Number.MAX_VALUE };
      return acc;
    }, {});

Test runner

Ready to run.

Testing in
TestOps/sec
Normal for loop over rows and columns
for (let i = 0; i < data; i++) {
  for (let j = 0; j < data[i]; i++) {
  	const value = data[i][j];
  	const columnType = columnTypes[j];
	const castedValue = columnType === 'integer' ? parseInt(value, 10) : parseFloat(value);
	
	if (!Number.isNaN(castedValue)) {
		const r = res[columnName];
		r.maximum = Math.max(columnMeta.maximum, castedValue);
		r.minimum = Math.min(columnMeta.minimum, castedValue);
	}
  }
}
ready
For-of over rows and columns
for (const row of data) {
  var columnIndex = 0;

  for (const value of row) {
  	const columnName = columnNames[columnIndex];  	
  	const columnType = columnTypes[columnIndex];
	const castedValue = columnType === 'integer' ? parseInt(value, 10) : parseFloat(value);
	
	if (!Number.isNaN(castedValue)) {
		const r = res[columnName];
		r.maximum = Math.max(r.maximum, castedValue);
		r.minimum = Math.min(r.minimum, castedValue);
	}
    columnIndex++;
  }
}
ready
Foreach
data.forEach(row => {
  var columnIndex = 0;

  row.forEach((value, columnIndex) => {
  	const columnName = columnNames[columnIndex];
  	const columnType = columnTypes[columnIndex];
	const castedValue = columnType === 'integer' ? parseInt(value, 10) : parseFloat(value);
	
	if (!Number.isNaN(castedValue)) {
		const r = res[columnName];
		r.maximum = Math.max(r.maximum, castedValue);
		r.minimum = Math.min(r.minimum, castedValue);
	}
  })
})
ready

Revisions

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