_.filter vs .filter (v5)

Revision 5 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 smallNumericArray = [0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 8, 10];
const numericThreshold = 1;
const numericPredicate = (val) => val > numericThreshold;
const numericPredicateInverse = (val) => val < numericThreshold;

const stringArray = [
	"APPLE",
	"ORANGE",
	"BANANA",
	"GRAPE",
	"MANGO",
	"PINEAPPLE",
	"STRAWBERRY",
];
const stringToExclude = "ORANGE";
const stringPredicate = (color) => color !== stringToExclude;

const objectArraySize = 100;
const objectArray = [];
const orientations = [
	"center",
	"top",
	"bottom",
	"left",
	"right",
	"top-left",
	"top-right",
];
for (let i = 0; i < objectArraySize; i++) {
	objectArray.push({
		id: `id_${i}`,
		token: `token_${i % 10}`, // Simulate filtering out specific tokens
		type: i % 5 === 0 ? "special" : "regular",
		value: Math.random() * 100,
		active: i % 3 === 0,
		orientation: orientations[i % orientations.length],
		matches: {
			// Simulating label.enforceOrientation
			center: i % orientations.length === 0,
			top: i % orientations.length === 1,
			bottom: i % orientations.length === 2,
		},
		// Method simulation
		isActive: function () {
			return this.active;
		},
		isSpecificType: function (t) {
			return this.type === t;
		},
	});
}

const specificTokenToExclude = "token_3";
const objectIdPredicate = (item) => item.token !== specificTokenToExclude;

const specificOrientation = "center";
const objectOrientationPredicate = (item) =>
	item.orientation === specificOrientation;
const objectNotOrientationPredicate = (item) =>
	item.orientation !== specificOrientation;

const enforcedOrientation = "top";
const objectEnforcedOrientationPredicate = (item) =>
	item.matches[enforcedOrientation];

const objectMethodPredicate = (item) => item.isActive();
const objectMethodWithArgPredicate = (item) => item.isSpecificType("special");

Test runner

Ready to run.

Testing in
TestOps/sec
_.filter
_.filter(smallNumericArray, numericPredicate);
_.filter(stringArray, stringPredicate);
_.filter(objectArray, objectIdPredicate);
_.filter(objectArray, objectOrientationPredicate);
_.filter(objectArray, objectEnforcedOrientationPredicate);
_.filter(objectArray, objectMethodPredicate);
ready
.filter
smallNumericArray.filter(numericPredicate);
stringArray.filter(stringPredicate);
objectArray.filter(objectIdPredicate);
objectArray.filter(objectOrientationPredicate);
objectArray.filter(objectEnforcedOrientationPredicate);
objectArray.filter(objectMethodPredicate);
ready

Revisions

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