Unlock the full potential of JavaScript with these expert strategies and code examples, from memoization to BigInt and beyond.
1. Memoization
Memoization is an optimization technique that stores the results of expensive function calls and returns the cached result when the same inputs occur again, improving performance.
Example:
const memoize = (func) => { const cache = {}; return (...args) => { const key = JSON.stringify(args); if (!cache[key]) { cache[key] = func(...args); } return cache[key]; };
}; const fibonacci = memoize((n) => n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2));
console.log(fibonacci(10)); // 55
2. Currying
Currying transforms a function with multiple arguments into a sequence of nesting functions. It’s useful for creating a higher-order function with multiple use cases.
Example:
const curry = (func) => (...args) => args.length >= func.length ? func(...args) : curry(func.bind(null, ...args)); const add = (a, b, c) => a + b + c;
const curriedAdd = curry(add); console.log(curriedAdd(1)(2)(3)); // 6
3. Proxy for Object Property Access:
The Proxy object is used to define custom behavior for fundamental operations (e.g., property lookup, assignment, enumeration, function invocation, etc.).
Example:
const handler = { get: (obj, prop) => prop in obj ? obj[prop] : `Property ${prop} not found.`,
}; const p = new Proxy({}, handler);
p.a = 1; console.log(p.a, p.b); // 1, Property b not found.
4. Using Generators for Iteration Control:
Generators are functions that can be exited and later re-entered, allowing for more control over the iteration process.
Example:
function* idGenerator() { let id = 1; while (true) { yield id++; }
} const gen = idGenerator();
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
5. Symbols for Unique Property Keys
Symbols are unique and immutable data types and can be used as object keys to avoid property name collisions.
Example:
const symbol1 = Symbol('identifier');
const symbol2 = Symbol('identifier'); console.log(symbol1 === symbol2); // false const obj = { }; console.log(obj[symbol1]); // value1
console.log(obj[symbol2]); // value2
6. Reflect API for Meta Programming:
The Reflect API provides methods for interceptable JavaScript operations, offering a more uniform interface for meta programming.
Example:
const obj = { a: 1 };
Reflect.defineProperty(obj, 'b', { value: 2 }); console.log(obj.b); // 2
7. Using Set for Unique Values:
Set is a collection of unique values. It can be used to remove duplicates from an array efficiently.
Example:
const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = new Set(numbers); console.log([...uniqueNumbers]); // [1, 2, 3, 4, 5]
8. WeakMap for Better Memory Management
WeakMap allows you to create a key-value pair where the keys are objects and the values can be arbitrary values. The keys are weakly referenced, aiding in garbage collection.
Example:
let obj = {};
const weakmap = new WeakMap();
weakmap.set(obj, { key: 'value' }); console.log(weakmap.get(obj)); // { key: 'value' }
obj = null; // Now `obj` and its associated data can be garbage collected
9. Using Promise.all for Parallel Promises
Promise.all lets you run multiple promises in parallel and wait for all of them to resolve.
Example:
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve) => setTimeout(resolve, 100, 'foo')); Promise.all([promise1, promise2, promise3]).then((values) => { console.log(values); // [3, 42, 'foo']
});
10. BigInt for Large Integer Operations
BigInt is a built-in object that provides a way to represent whole numbers larger than 2^53 – 1, which is the largest number JavaScript can reliably represent with the Number primitive.
Example:
const a = 9007199254740991n;
const b = 1n; console.log(a + b); // 9007199254740992n
Conclusion
By mastering these advanced JavaScript tricks, experienced developers can create more efficient and maintainable and with better performance. These techniques not only demonstrate your knowledge of JavaScript, but also enable you to solve complex problems with elegance and finesse. As you continue to explore the language, remember that practice and Experimentation is the key to becoming a proficient JavaScript developer.
Discover more from Coursity
Subscribe to get the latest posts sent to your email.