JavaScript Testing And Optimization (original) (raw)
What is the primary purpose of Jest in JavaScript?
- Unit testing JavaScript code
What does the following code in Jest do?
JavaScript `
const mockFn = jest.fn(); mockFn('test'); console.log(mockFn.mock.calls[0][0]);
`
- Returns the number of function calls
- Logs all arguments passed to the mock function
- Logs the first argument of the first call
What is the purpose of JavaScript's garbage collector?
- Frees memory of unused variables
- Automatically removes unreferenced objects to free memory
What is the most common algorithm used in JavaScript garbage collection?
What is lazy loading in web development?
- Preloading all resources at once
- Caching resources for later use
- Loading resources in parallel
- Delaying the loading of resources until needed
Which attribute enables lazy loading of images in HTML?
What is the main purpose of debouncing in JavaScript?
- To limit function calls to a fixed rate
- To delay a function call until a pause in events
- To cache function results
What is throttling in JavaScript?
- Combining multiple function calls into one
- Delaying function calls until events stop
- Ensuring a function is called at most once in a specified time period
What is the key difference between debouncing and throttling?
- Throttling is faster than debouncing.
- Debouncing is used for lazy loading, while throttling is for memory management.
- Debouncing waits for a pause in events, while throttling ensures periodic execution.
- Both are identical and interchangeable.
What does the following Jest test case do?
JavaScript `
test('adds 1 + 2 to equal 3', () => { expect(1 + 2).toBe(3); });
`
- Logs the result of the addition
- Throws an error if the result is not 3
- Mocks the addition function
- Asserts that the sum of 1 and 2 equals 3
There are 10 questions to complete.
Take a part in the ongoing discussion