MapReduce vs.

Benchmark created on


Setup

const items = 
 ['0', '3', '4', '5', '6', {data: 4}, undefined, {data: undefined}, '3', '4', '5', '6', {data: 4}, undefined, {data: undefined}, '3', '4', '5', '6', {data: 4}, undefined, {data: undefined}, '3', '4', '5', '6', {data: 4}, undefined, {data: undefined}, '3', '4', '5', '6', {data: 4}, undefined, {data: undefined}, '3', '4', '5', '6', {data: 4}, undefined, {data: undefined}, '3', '4', '5', '6', '0', {data: 4}, undefined, {data: undefined}, '3', '4', '5', '6', {data: 4}, undefined, {data: undefined}
];

const getItem = (item) => {
  if(typeof item === 'object'){
  	return +item.data;
  }
  return +item;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Filtering and mapping
items.map(getItem).filter(item => item != null);
ready
Reducing (immutably)
items.reduce((allItems, item) => {
  const val = getItem(item);
  if(val != null){
  	return [...allItems, val];
  }
}, []);
ready
mapReduce
const mapReduce = (items,callback) => {
  const filteredItems = [];
  let somethingChanged = false;
  for (let i = 0; i < items.length; i += 1) {
    const item = items[i];
    if (item != null) {
      const newItem = callback(item, i, items);

      if (item !== newItem) {
        somethingChanged = true;
      }
      if (newItem == null) {
        somethingChanged = true;
      } else {
        filteredItems.push(newItem);
      }
    } else {
      somethingChanged = true;
    }
  }
  return somethingChanged ? filteredItems : items;
};

mapReduce(items, getItem);
ready

Revisions

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