Chained loop methods

Benchmark created on


Setup

const files = [
	{
		name: "dir1",
		isFile: () => false,
	},
	{
		name: "dir2",
		isFile: () => false,
	},
	{
		name: "file1.md",
		isFile: () => true,
	},
	{
		name: "file2.md",
		isFile: () => true,
	},
	{
		name: "file3.js",
		isFile: () => true,
	},
	{
		name: "file4.js",
		isFile: () => true,
	},
	{
		name: "file5.js",
		isFile: () => true,
	},
	{
		name: "file6.js",
		isFile: () => true,
	},
	{
		name: "file7.js",
		isFile: () => true,
	},
	{
		name: "file8.js",
		isFile: () => true,
	},
	{
		name: "file9.js",
		isFile: () => true,
	},
	{
		name: "file10.js",
		isFile: () => true,
	},
];

Test runner

Ready to run.

Testing in
TestOps/sec
Single loop
const filteredFiles = [];

files.forEach(item => {
	if (!item.isFile()) {
		return;
	}

	if (!item.name.endsWith(".js")) {
		return;
	}

	filteredFiles.push(`src/js/${item.name}`);
});
ready
Chained loop methods
const filteredFiles = files
	.filter((item) => item.isFile())
	.filter((item) => item.name.endsWith(".js"))
	.map((item) => `src/js/${item.name}`);
ready

Revisions

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