Performance: path.extname for file extensions
This affects the speed of extracting file extensions during file path processing, impacting script execution time.
Jump into concepts and practice - no test required
const ext = path.extname(filename);
const ext = filename.split('.').pop();| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| String.split + pop | 0 | 0 | 0 | [X] Bad |
| path.extname | 0 | 0 | 0 | [OK] Good |
path.extname('example.txt') return in Node.js?path.extname method extracts the file extension including the dot from a filename string.path module?path module provides the method extname to get file extensions.path.extname('filename'). Other options are invalid method names.const path = require('path');
console.log(path.extname('archive.tar.gz'));path.extname method returns the substring from the last dot to the end of the string.const path = require('path');
const ext = path.extname('document');
console.log(ext);path.extname returns an empty string when no extension is found, not an error or undefined.path.extname to do this?const path = require('path');
const files = ['app.js', 'index.html', 'script.ts', 'readme'];
const jsFiles = files.filter(???);
console.log(jsFiles);path.extname(file) === '.js'. Other options either miss the dot, use wrong case, or check for empty extension.