npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-render-to-string-async

v0.0.3

Published

Non-blocking asynchronous alternative to `React.renderToString` and `React.renderToStaticMarkup`.

Downloads

8

Readme

React Render To String Async

Non-blocking asynchronous alternative to React.renderToString and React.renderToStaticMarkup.

import React from "react";
import { renderToString, renderToStaticMarkup } from "react-dom/server";
import {
  renderToStringAsync,
  renderToStaticMarkupAsync,
} from "react-render-to-string-async";

const root = React.createElement("div");

const syncStr = renderToString(root);
const asyncStr = await renderToStringAsync(root);

console.log(syncStr === asyncStr); // true

const syncStaticMarkup = renderToStaticMarkup(root);
const asyncStaticMarkup = await renderToStaticMarkupAsync(root);

console.log(syncStaticMarkup === asyncStaticMarkup); // true

Why?

React.renderToString (and React.renderToStaticMarkup) can be very slow on large component trees and worse still it's blocking, meaning tasks waiting in the event queue won't be executed until it's fully complete.

React.renderToStringAsync (and React.renderToStaticMarkupAsync) will perform work in chunks, yielding to the event loop, allowing rendering to be interleaved with other tasks.

NOTE: React also offers React.renderToNodeStream (and React.renderToStaticNodeStream) which allows you to send individual chunks, one at a time and potentially yield to the event loop, however often this isn't suitable when server side rendering, as you need to finish the render before you can determine the appropriate response code, e.g. sending a 404 if the render eventually says the page is not found.

How?

Nothing clever is going on here, we're merely wrapping React.renderToNodeStream (and React.renderToStaticNodeStream) in a promise API.

This means, just like React.renderToString, backpressure is not handled so you should expect the entire html string in memory at once.

API

type renderToStringAsync = (
  element: React.ReactElement,
  options?: Options
) => Promise<string>;

type renderToStaticMarkupAsync = (
  element: React.ReactElement,
  options?: Options
) => Promise<string>;

interface Options {
  scheduleFn?: (fn) => any; // defaults to `setImmediate`
}

Performance

DISCLAIMER: This is purely an experiment and has not yet been proven in a production environment.

Hypothesis

By sharing the CPU time more evenly among requests, we should see a lower standard deviation in response times. Cheaper requests should benefit the most, as they have an opportunity to finish without waiting for more expensive requests to complete.

However, the median response time is expected to increase, as a particular request does not have unrestricted access to the CPU.

Synthetic Benchmarks

Very surprisingly, renderToStringAsync (setImmediate) appears to be faster than React.renderToString when given a large tree. It's slower however on smaller trees. See Benchmarks.

Additionally, it appears renderToStringAsync (process.nextTick) and renderToStringAsync ((fn) => fn()) are faster still, however they do not yield to the event loop; perhaps a nice alternative if yielding in fact results in poorer overall performance.