Javascript Interview Questions: Pagination, Memoization, Event Bubbling
Photo by Claudio Schwarz on Unsplash
1. What is pagination?
What is Pagination? And How to Implement it on Your Website — SEOptimer
Pagination is a sequence of pages which are connected and have similar content. It is important to note that even when the content on a section of a page is split into distinct pages, we will still define that as pagination.
Why use pagination?
- Better user experience
- Easier navigation
Real-Time Scenario: Let’s take the amazon website or Flipkart website for displaying available products in their database. Let suppose they have 1 million products with them. If they are trying to show all the items at a time, the customer must wait more time like one day to see all the product lists.
Introduction to Pagination in JavaScript
JavaScript Pagination concept is applied for moving among the pages with First, Next, Previous and Last buttons or links. Pagination’s main motto is to move among the content immediately by clicking links or buttons. Pagination has multiple links or buttons provided for access First, Next, Previous and Last content. Create First, Next, Previous and Last buttons in JavaScript we have used different JavaScript functions.
How Should we Tackle this Situation?
Instead of showing all the items at a time, we can show them 50 to 100 items at a time by using a list of link buttons. If the first 50 to 100 items do not fulfil the need of a client, we then move to the next 50 to 100.
|
|
Example:
Hung, Chien-Hsiang (chienhsiang-hung.github.io)
Hung, Chien-Hsiang (chienhsiang-hung.github.io)
|
|
chienhsiang-hung.github.io/EmbeddingMedium.js at main · chienhsiang-hung/chienhsiang-hung.github.io
2. What is Memoization?
In programming, memoization is an optimization technique that makes applications more efficient and hence faster. It does this by storing computation results in cache, and retrieving that same information from the cache the next time it’s needed instead of computing it again.
In simpler words, it consists of storing in cache the output of a function, and making the function check if each required computation is in the cache before computing it.
A cache is simply a temporary data store that holds data so that future requests for that data can be served faster.
Memoization is a simple but powerful trick that can help speed up our code, especially when dealing with repetitive and heavy computing functions.
3. Bubbling and capturing
Bubbling
The bubbling principle is simple.
When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.
Let’s say we have 3 nested elements FORM > DIV > P
with a handler on each of them:
|
|
A click on the inner <p>
first runs onclick
:
- On that
<p>
. - Then on the outer
<div>
. - Then on the outer
<form>
. - And so on upwards till the
document
object.
So if we click on <p>
, then we’ll see 3 alerts: p
→ div
→ form
.
The process is called “bubbling”, because events “bubble” from the inner element up through parents like a bubble in the water.
Almost all events bubble.
The key word in this phrase is “almost”.
For instance, a
focus
event does not bubble. There are other examples too, we’ll meet them. But still it’s an exception, rather than a rule, most events do bubble.
Contact me: Hung, Chien-Hsiang (chienhsiang-hung.github.io)