Deep Dive into Page Lifecycle API

Browser API Every Frontend Developer Should Know

Viduni Wickramarachchi
Photo by Jeremy Perkins on Unsplash

As users, we always like to multi-task while browsing web pages. So it’s common to have several browser tabs open as it helps to get things done in parallel. However, at the same time, each of these tabs consumes system resources like memory and CPU.

Since it’s impossible to limit users from opening new browser tabs and leaving them behind, browsers have taken several measures to deallocate resources when the browser tabs aren’t active.

Modern browsers today will sometimes suspend pages or discard them entirely when system resources are constrained — Philip Walton

It is not entirely true that the browser takes care of everything. Besides, these browser interventions have a direct impact on JavaScript execution. The good news is, almost all the modern browsers expose these interventions as events via the Page Lifecycle API.

As the name suggests, the Page Lifecycle API exposes the web page lifecycle hooks to JavaScript. However, it isn’t an entirely new concept. Page Visibility API existed for some time, revealing some of the page visibility events to JavaScript.

However, if you happen to choose between these two, it’s worth mentioning some of the limitations of the Page Visibility API.

  • It only provides visible and hidden states of a web page.
  • It can not capture pages discarded by the operating system (Android, IOS, and the latest Windows systems can terminated background processes to preserve system resources).

Let’s take a look at the page lifecycle states exposed by the Page Lifecycle API.

There are six states introduced in the API. Out of them, two states are quite relevant to us.

  • FROZEN — Lifecycle state for CPU suspension (Hidden pages will be frozen to conserve resources).

If a webpage has been hidden for a long time and the user does not close the page, the browser will freeze it and move the page into this state. However, the running tasks will continue until completed. But timers, callback function executions, and DOM operations will be stopped to release the CPU.

Chrome Resource consumption

When I look at the Chrome Browser resource consumption on my PC, I observed that the two active tabs consume 14.7% and 11% of the CPU while the Frozen ones consume nearly 0%.

  • DISCARDED — Frozen frames are moved to the Discarded state to conserve resources.

Suppose a webpage is in a frozen state for a long time. In that case, the browser will automatically unload the page into the discarded state, releasing some memory. And If the user re-visits a discarded page, the browser will reload the page to return to the Active State.

It’s worth noticing that the users will typically experience the discarded state in devices with resource constraints.

Apart from the above two states, there are four other states introduced in the API as,

  • ACTIVE — Page is visible and has input focus.
  • PASSIVE — Page is visible but doesn’t have input focus.
  • HIDDEN — Page is not visible (Not frozen either).
  • TERMINATED — Page is unloaded and cleared from memory.

You can find the lifecycle states and the transition in detail by looking at the following diagram.

Author: Shantun Parmar

108 thoughts on “Deep Dive into Page Lifecycle API

  1. Greetings! Very useful advice within this article!
    It is the little changes that will make the largest changes.

    Thanks a lot for sharing!

  2. I blog quite often and I really thank you for your content.

    This great article has really peaked my interest. I will bookmark your site and keep checking for new information about once a week.
    I subscribed to your Feed too.

    Also visit my blog post Jolly CBD Gummies

  3. Very nice post. I just stumbled upon your weblog and wanted to say that I’ve truly enjoyed surfing around your blog posts.
    After all I’ll be subscribing to your rss feed and I hope you write
    again very soon!

    Here is my web blog Keto ES

  4. In November 2020, Maryland voters authorized “sports and events betting for the key objective of raising income for education”
    by about a 2-1 margin.

  5. Having read this I believed it was extremely enlightening.
    I appreciate you taking the time and effort to put this article together.

    I once again find myself personally spending a lot of time both reading and posting comments.
    But so what, it was still worth it!

    Feel free to surf to my web blog – CamHandy Rear View DVR

  6. Useful info. Fortunate me I discovered your website unintentionally, and I am stunned why this twist of fate
    didn’t happened earlier! I bookmarked it.

  7. This is the right site for anybody who hopes to understand
    this topic. You know a whole lot its almost hard to argue
    with you (not that I really would want to?HaHa). You definitely put a brand
    new spin on a topic that’s been written about for years.
    Excellent stuff, just great!

    Feel free to visit my blog :: CamHandy

  8. I like the helpful info you provide in your articles.
    I’ll bookmark your blog and check again here frequently.
    I am quite sure I’ll learn many new stuff right here! Best of luck for the next!

  9. I simply wanted to construct a simple message in order to appreciate you for all the marvelous guidelines
    you are writing at this website. My extended internet research has at
    the end been compensated with pleasant details to talk about
    with my partners. I ‘d believe that many of us visitors are truly blessed to live in a fine
    website with so many awesome professionals with beneficial tips and
    hints. I feel rather fortunate to have encountered the weblog and
    look forward to so many more pleasurable minutes reading
    here. Thanks a lot once more for everything.

    Here is my blog – Full Body Male Enhancement

  10. Hmm it appears like your blog ate my first comment (it was extremely long) so I guess I’ll just sum it up what I submitted and say,
    I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to the whole thing.

    Do you have any points for rookie blog writers? I’d genuinely appreciate

    Here is my site: Luminas Pain Patch Reviews

  11. Every weekend i used to go to see this website, for the reason that i want enjoyment,
    for the reason that this this web site conations really fastidious funny material too.

  12. I don’t know whether it’s just me or if perhaps everyone else encountering problems with your blog.
    It looks like some of the text in your posts are running off the screen. Can somebody else please comment
    and let me know if this is happening to them as well?
    This may be a issue with my web browser because I’ve had this happen previously.
    Many thanks

    My site; Full Body Male Enhancement Pills

  13. I was curious if you ever considered changing the structure of your blog?
    Its very well written; I love what youve got to
    say. But maybe you could a little more in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having 1 or 2 pictures.
    Maybe you could space it out better?

  14. We’re a group of volunteers and opening a brand new scheme in our community.
    Your website offered us with valuable information to work on. You have performed a
    formidable process and our whole community will likely be thankful to you.

  15. Very nice post. I simply stumbled upon your blog and wished to mention that
    I’ve truly loved browsing your weblog posts. After all I will be subscribing to
    your rss feed and I am hoping you write once more soon!

  16. In fact when someone doesn’t be aware of after that
    its up to other visitors that they will help, so here it occurs.

  17. Attractive section of content. I just stumbled upon your web site and in accession capital to assert that I get actually enjoyed
    account your blog posts. Anyway I’ll be subscribing to your feeds and
    even I achievement you access consistently quickly.

  18. You’re so cool! I don’t suppose I have read through a single thing like that before.
    So good to find another person with some unique thoughts on this issue.
    Really.. many thanks for starting this up. This website
    is one thing that is needed on the web, someone with a little originality!

    My web blog – Luminas Pain Patch Reviews

  19. Good day! Do you know if they make any plugins to safeguard against
    hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

  20. Wow that was unusual. I just wrote an very long comment
    but after I clicked submit my comment didn’t show up.

    Grrrr… well I’m not writing all that over again. Anyways, just wanted to say excellent blog!

  21. I’m no longer positive the place you are getting your information, however good topic.
    I must spend a while finding out much more or understanding more.
    Thank you for excellent information I was searching for this info for
    my mission.

  22. Very nice post. I just stumbled upon your blog and wished to say that I’ve truly enjoyed surfing around your blog posts.
    After all I’ll be subscribing in your rss feed and I’m hoping
    you write once more soon!

  23. I do not even know how I finished up here, however I assumed this publish was
    good. I don’t realize who you are however definitely you’re going to a well-known blogger if you happen to aren’t already.

    Here is my web-site; Jolly CBD

  24. My partner and I absolutely love your blog and find many of your post’s to be what precisely I’m looking for.
    Does one offer guest writers to write content
    available for you? I wouldn’t mind creating a post
    or elaborating on most of the subjects you write in relation to here.
    Again, awesome blog!

    Also visit my website :: free slots tournaments

  25. What i do not understood is in reality how you are no longer really much more smartly-appreciated than you
    may be now. You are so intelligent. You recognize thus significantly with regards to this matter, made me individually believe it from so many numerous angles.
    Its like men and women don’t seem to be fascinated until it is something to do with Lady gaga!

    Your personal stuffs great. Always care for it up!

  26. Pretty section of content. I just stumbled upon your web
    site and in accession capital to say that I get in fact enjoyed account
    your weblog posts. Anyway I’ll be subscribing in your feeds and even I success
    you get entry to constantly fast.

    my website; Noble

  27. An outstanding share! I have just forwarded this onto a co-worker who had been conducting a little homework on this.

    And he in fact ordered me breakfast due to the fact that I discovered it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending some time to discuss this issue here on your site.

    Also visit my site; Small Business startup

  28. I was wondering if you ever thought of changing the structure of your website?
    Its very well written; I love what youve got to
    say. But maybe you could a little more in the way of content
    so people could connect with it better. Youve got an awful lot of text
    for only having 1 or two pictures. Maybe you could space it out better?

    Review my homepage – play in online casino

  29. Definitely believe that which you stated. Your favorite justification appeared
    to be on the net the easiest thing to take note of. I say to you,
    I certainly get annoyed at the same time as folks consider worries that they just don’t recognize about.
    You managed to hit the nail upon the top as
    well as defined out the entire thing with no need side effect , people could take a signal.

    Will likely be again to get more. Thank you

  30. As far as the US goes, regulation efforts have been slow due
    to fears of sports betting corrupting athletes and encouraging point-shaving scandals.

Thanks for your support, You may click on ads to encourage us which assits to writers.

Leave a Reply

Your email address will not be published.