Created
July 8, 2021 22:41
-
-
Save chadwithuhc/65b8afa9431795e29f386e1f6a39aae0 to your computer and use it in GitHub Desktop.
switch statement vs object lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Q: Which would you prefer to get a mapped value? | |
// with a `switch` statement | |
export function getHeaderValue(headerName: string): string { | |
switch (headerName) { | |
case "content-type": | |
return headers.contentType(); | |
case "content-length": | |
return headers.contentLength(); | |
case "content-md5": | |
return headers.contentMd5Hash(); | |
case "etag": | |
return headers.etag(); | |
case "last-modified": | |
return headers.lastModified(); | |
case "accept-ranges": | |
return headers.acceptRanges(); | |
case "cache-control": | |
return headers.cacheControl(); | |
case "content-disposition": | |
return headers.contentDisposition(); | |
default: | |
return headerName; | |
} | |
} | |
// with an object lookup | |
export function getHeaderValue(headerName: string): string { | |
return { | |
"content-type": headers.contentType(), | |
"content-length": headers.contentLength(), | |
"content-md5": headers.contentMd5Hash(), | |
"etag": headers.etag(), | |
"last-modified": headers.lastModified(), | |
"accept-ranges": headers.acceptRanges(), | |
"cache-control": headers.cacheControl(), | |
"content-disposition": headers.contentDisposition() | |
}[headerName] || headerName; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment