_.any vs .some (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.6/underscore-min.js"></script>

Setup

const arraySize = 100;

// 1. Array for truthiness check (approximates `_.any(arrOfValues)`)
const mixedTruthinessArray = [];
for (let i = 0; i < arraySize; i++) {
	if (i === Math.floor(arraySize / 2)) {
		// Ensure one truthy value
		mixedTruthinessArray.push(true);
	} else if (i === Math.floor(arraySize / 1.5)) {
		// Ensure one truthy string value
		mixedTruthinessArray.push("hello");
	} else {
		mixedTruthinessArray.push(i % 5 === 0 ? 0 : false); // Mix of falsy values
	}
}
const truthyPredicate = (value) => !!value;

// 2. Array of objects for predicate testing
const arrayOfObjects = [];
for (let i = 0; i < arraySize; i++) {
	arrayOfObjects.push({
		id: i,
		type: i % 10 === 0 ? "special" : "normal",
		value: Math.random() * 100,
		isHidden: i % 7 === 0,
		stringValue: (i * 1.5).toFixed(2),
	});
}

// Simulates `!getIsHidden(item)`
const isVisiblePredicate = (item) => !item.isHidden;
// Simulates `s.type === 'R^2' && s.value !== undefined && s.value < 0`
const complexPropertyPredicate = (item) =>
	item.type === "special" && item.value < 10 && item.id > 5;
// Simulates `parseFloat(v) <= 0`
const stringParsePredicate = (item) => parseFloat(item.stringValue) < 50;

const allHiddenItems = Array(arraySize)
	.fill(null)
	.map((_, i) => ({ id: i, isHidden: true }));

Test runner

Ready to run.

Testing in
TestOps/sec
_.any
_.any(mixedTruthinessArray, truthyPredicate);
_.any(arrayOfObjects, isVisiblePredicate);
_.any(allHiddenItems, isVisiblePredicate);
_.any(arrayOfObjects, complexPropertyPredicate);
_.any(arrayOfObjects, stringParsePredicate);
ready
.some
mixedTruthinessArray.some(truthyPredicate);
arrayOfObjects.some(isVisiblePredicate);
allHiddenItems.some(isVisiblePredicate);
arrayOfObjects.some(complexPropertyPredicate);
arrayOfObjects.some(stringParsePredicate);
ready

Revisions

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