Performance: URLSearchParams for query strings
This affects how query strings are parsed and serialized, impacting server-side processing speed and memory usage.
Jump into concepts and practice - no test required
const query = 'name=alice&age=30'; const params = new URLSearchParams(query); const name = params.get('name'); const age = params.get('age');
const query = 'name=alice&age=30'; const params = {}; query.split('&').forEach(pair => { const [key, value] = pair.split('='); params[key] = decodeURIComponent(value); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual string split and decode | 0 | 0 | 0 | [X] Bad |
| URLSearchParams native API | 0 | 0 | 0 | [OK] Good |
URLSearchParams class in Node.js primarily help you do?URLSearchParams object from a query string in Node.js?new keyword and accepts a query string like '?name=John&age=30'.new or passing an object directly without conversion are invalid.new with query string [OK]const params = new URLSearchParams('color=blue&size=medium');
params.set('size', 'large');
console.log(params.toString());set method updates the value of the 'size' parameter from 'medium' to 'large'.toString() method returns the query string with updated parameters in the order they were added.const params = new URLSearchParams('page=1&limit=10');
params.append('page', 2);
console.log(params.get('page'));{ search: 'books', page: 2, filter: '' } but want to exclude empty values. Which code correctly creates the query string using URLSearchParams?