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

fast-iso-string

v1.0.0

Published

A faster implementation of new Date().toISOString().

Downloads

227

Readme

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

A way to get the ISO String about 120%~130% faster than using new Date().toISOString().

Here's a C++ code rewrite to TypeScript from V8 to get a faster ISO String format date.

See here the original implementation.

Usage

First, install the library:

npm i fast-iso-string

Then, you have two exposed methods that you can use:

  • fastISOString: That returns the current date as ISO String.
  • fromUnixToISOString: To parse a unix time that you already have.

And then use like this:

import { fastISOString } from 'fast-iso-string';

-const isoString = new Date().toISOString();
+const isoString = fastISOString();

Benchmarks

Above, the difference of calling new Date().toISOString() or using the library.

new Date().toISOString() x 1,419,493 ops/sec ±2.00% (85 sampled runs)
fastISOString() x 3,306,564 ops/sec ±2.75% (85 sampled runs)

Why is fast?

Well, I have two reasons:

  • Avoiding creating new Date(), actually, using just Date.now() is almost 100% faster than calling new Date().
  • JS->C++ cost, this is a hidden cost of calling V8 to perform that operation, I don't know exactly the cost but I think that contribute to the slowdown.

Is fair to compare new Date against Date.now?

You might consider this benchmark unfair because I'm using Date.now() instead of new Date(). Actually yeah it's not fair, if we use fromUnixToISOString(new Date()) it might be slower as per this and this.

But otherwise, do you know how to get ISO String without calling new Date()? I don't know, so JavaScript for us to use the Date object, so I just skip that runtime cost and go directly to what I want, the formatted ISO String, which format you get the start date in doesn't matter.

This also leaves a question: so everything I'm doing with new Date().MY_METHOD could be faster? Maybe yes. If you parse unix time and extract the information in whatever format you want it could be faster. I just created one of the Date methods, but you can try and see if you get faster results with other methods.

Should I use this library?

If you're doing a lot of operations that need to use the ISO String, maybe you should.

It's not because I'm saying that one method is faster than the other, that this will bring some performance improvement to your workflow, the best thing you can do is to do a benchmarking, to learn more, see Preparing and Evaluating Benchmarks by @RafaelGSS.

Thanks

I need to thank @RafaelGSS for inspiring the creation of this library for his work on performance within NodeJs.