How does chaining array methods like map and filter affect performance?
- Negatively impacts performance due to multiple iterations
- Positively impacts performance by optimizing the code
- No significant impact on performance
- Only affects readability, not performance
Chaining array methods like map and filter can negatively impact performance due to multiple iterations. Each method creates a new array, and chaining them leads to intermediate arrays, causing additional iterations. This can be inefficient, especially for large datasets.
Imagine processing a shopping cart array to calculate the total price. Which method would efficiently achieve this?
- map
- reduce
- filter
- forEach
The most efficient method for calculating the total price in a shopping cart array is reduce. The reduce method iterates over each element of the array and accumulates a result. In this case, it can sum up the prices to provide the total cost.
What is a practical use case of currying in JavaScript?
- Simplifying function composition
- Handling asynchronous operations
- Memoization
- Event handling in UI
Currying simplifies function composition by breaking down functions into smaller, reusable units. This is useful for creating more modular and maintainable code, especially in functional programming paradigms.
How can dynamic imports be used to implement feature flags in a large-scale web application?
- Load all features during the initial page load.
- Use dynamic imports to conditionally load features based on feature flags.
- Hardcode feature flags directly in the application code.
- Use static imports to include all features in the main bundle.
Dynamic imports enable the implementation of feature flags by allowing the loading of features based on runtime conditions. This flexibility helps in enabling or disabling specific features without changing the core codebase.
Can a default export be imported with any name?
- Yes
- No
- Only in certain cases
- Depends on the file structure
In ES6, a default export can be imported with any name. The imported name is not dictated by the exporting module. It allows flexibility in naming while importing, making it more convenient.
When using Symbols for object properties, they are not included in __________ enumerations.
- for...in'
- forEach'
- Object.keys()'
- Object.getOwnPropertySymbols()'
Object properties with Symbols as keys are not included in for...in enumerations. To get all property keys (including Symbols), you can use Object.getOwnPropertySymbols() in addition to other methods. Example: const symbols = Object.getOwnPropertySymbols(myObject);
Destructuring an object within a _________ allows for directly mapping object properties to function parameters.
- Callback function
- Template literal
- Arrow function
- Promise
Destructuring an object within an "Arrow function" allows for directly mapping object properties to function parameters. Arrow functions provide a concise syntax, and when combined with destructuring, you can streamline the mapping of object properties to function parameters.
To check if a Map contains a certain key, use the _________ method.
- hasKey
- checkKey
- has
- includes
The correct method is has. In JavaScript, the Map object has a has method that returns a boolean indicating whether the map contains a specified key. Example: myMap.has('key').
How does the event loop contribute to the non-blocking behavior in a Node.js server handling multiple I/O operations?
- The event loop allows Node.js to delegate I/O operations to background threads or external processes, preventing them from blocking the main thread. The event loop ensures that once I/O operations complete, their callbacks are queued for execution, maintaining non-blocking behavior.
- The event loop in Node.js efficiently manages I/O operations by offloading them to background threads or external processes. This prevents the main thread from being blocked, and the event loop ensures that once I/O operations complete, their callbacks are prioritized for execution.
- Node.js utilizes the event loop to offload I/O operations, preventing them from blocking the main thread. The event loop ensures that once I/O operations are finished, their callbacks are queued for execution, contributing to the non-blocking nature of Node.js servers.
- The event loop in Node.js handles I/O operations asynchronously, preventing them from blocking the main threaOnce I/O operations are complete, their callbacks are queued and prioritized for execution, maintaining non-blocking behavior.
The event loop plays a crucial role in ensuring the non-blocking behavior of Node.js servers handling multiple I/O operations. It offloads I/O operations to background threads or external processes, preventing the main thread from being blocked. The event loop then manages the completion of I/O operations by queuing their callbacks for execution, contributing to the overall efficiency of handling concurrent I/O tasks.
In a WeakSet, the existence of an object in the set does not prevent it from being ________ by the garbage collector.
- Referenced
- Mutated
- Duplicate
- Garbage collected
The objects in a WeakSet are weakly held, similar to WeakMap, allowing them to be garbage collected even if they are present in the WeakSet. This is useful for managing sets of objects when their presence should not hinder garbage collection.