Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
A tiny (414B) and fast utility to convert milliseconds to and from strings.
@lukeed/ms is a lightweight utility for converting milliseconds to human-readable strings and vice versa. It is useful for handling time durations in a more readable format.
Convert milliseconds to human-readable string
This feature allows you to convert a duration in milliseconds to a human-readable string format. For example, 1500 milliseconds is converted to '1.5s'.
const ms = require('@lukeed/ms');
console.log(ms(1500)); // '1.5s'
Convert human-readable string to milliseconds
This feature allows you to convert a human-readable string representing a duration back to milliseconds. For example, '1.5s' is converted to 1500 milliseconds.
const ms = require('@lukeed/ms');
console.log(ms('1.5s')); // 1500
The 'ms' package provides similar functionality to @lukeed/ms, allowing conversion between milliseconds and human-readable strings. It is widely used and has a similar API, making it a good alternative.
The 'pretty-ms' package focuses on converting milliseconds to human-readable strings with more customization options. It offers more formatting options compared to @lukeed/ms, making it suitable for more complex use cases.
A tiny (414B) and fast utility to convert milliseconds to and from strings.
NOTICE: This is a fork of vercel/ms!
In June 2019, I opened a PR with signficiant performance and code size improvements. After nearly 2 years of silence, it was eventually closed. :cry: A year into my wait, I started anew (this repo), hoping to improve upon my own improvements.
This module is delivered as:
dist/index.js
dist/index.mjs
dist/index.min.js
$ npm install --save @lukeed/ms
import { parse, format } from '@lukeed/ms';
// string => number
parse('2 days'); //=> 172800000
parse('1d'); //=> 86400000
parse('10h'); //=> 36000000
parse('2.5 hrs'); //=> 9000000
parse('2h'); //=> 7200000
parse('1m'); //=> 60000
parse('5s'); //=> 5000
parse('1y'); //=> 31557600000
parse('100'); //=> 100
parse('-3 days'); //=> -259200000
parse('-1h'); //=> -3600000
parse('-200'); //=> -200
// number => string
format(60000); //=> '1m'
format(2 * 60000); //=> '2m'
format(-3 * 60000); //=> '-3m'
format(parse('10 hours')); //=> '10h'
// number => string (long)
format(60000, true); //=> '1 minute'
format(2 * 60000, true); //=> '2 minutes'
format(-3 * 60000, true); //=> '-3 minutes'
format(parse('10 hours'), true); //=> '10 hours'
Returns: Number
Parses the input string, returning the number of milliseconds.
Type: String
The human-readable time string; eg: 10min
, 10m
, 10 minutes
.
Returns: Number
Formats the millisecond count to a human-readable time string.
Important: The output will be rounded to the nearest whole integer.
Type: Number
The number of milliseconds.
Type: Boolean
Default: false
Whether or not the output should use the interval's long/full form; eg hour
or hours
instead of h
.
Note: When
long
, the count and interval will be separated by a single space.
Also, whenlong
, the interval may be pluralized; eg1 second
vs2 seconds
.
Running on Node.js v12.18.4
Validation :: parse
✔ lukeed/ms
✔ zeit/ms
Benchmark :: "parse"
lukeed/ms x 351,319 ops/sec ±0.31% (96 runs sampled)
zeit/ms x 245,576 ops/sec ±1.66% (94 runs sampled)
Benchmark :: "parse" (long)
lukeed/ms x 335,538 ops/sec ±0.50% (94 runs sampled)
zeit/ms x 265,410 ops/sec ±1.72% (95 runs sampled)
Validation :: format
✔ lukeed/ms
✔ zeit/ms
Benchmark :: "format"
lukeed/ms x 4,109,440 ops/sec ±0.35% (94 runs sampled)
zeit/ms x 3,420,198 ops/sec ±1.61% (94 runs sampled)
Benchmark :: "format" (long)
lukeed/ms x 3,402,872 ops/sec ±0.14% (97 runs sampled)
zeit/ms x 1,344,908 ops/sec ±3.68% (96 runs sampled)
This is obviously a fork of zeit/ms.
I opened a PR in June 2019 that introduced significant performance gains and code reduction — it was ignored for nearly two years. This repository is a from-scratch re-implementation that takes the goals of that PR a bit further.
MIT © Luke Edwards
FAQs
A tiny (414B) and fast utility to convert milliseconds to and from strings.
The npm package @lukeed/ms receives a total of 723,846 weekly downloads. As such, @lukeed/ms popularity was classified as popular.
We found that @lukeed/ms demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.