flatMap vs filter

Benchmark created on


Setup

const testHeaders = `
Content-Type: application/json
Authorization: Bearer token123
Accept-Language: en-US
User-Agent: Mozilla/5.0
Cache-Control: no-cache
Connection: keep-alive
Host: example.com
Accept: */*
Content-Length: 123
`

Test runner

Ready to run.

Testing in
TestOps/sec
flatMap
const headersWithFlatMap = Object.fromEntries( testHeaders .split(/\r?\n/) .flatMap((line) => { const match = /^([\w-]+): (.+)$/.exec(line); return match ? [[match[1], match[2]]] : []; }) );
ready
filter
const headersWithMapFilter = Object.fromEntries( testHeaders .split(/\r?\n/) .map((line) => { const match = /^([\w-]+): (.+)$/.exec(line); return match ? [match[1], match[2]] : null; }) .filter(Boolean) );
ready

Revisions

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