Skip to content

9a8ri3L/sizeof

Repository files navigation

GitHub Release NPM License GitHub forks GitHub stars Codacy Badge GitHub Downloads (all assets, all releases)

GitHub commits since latest release GitHub contributors npm bundle size (scoped)

sizeof

A utility function that calculates the size or the length of various types.

install

npm i @g-lib/sizeof
yarn add @g-lib/sizeof
pnpm add @g-lib/sizeof

Usage:

import sizeof from "@g-lib/sizeof";

// or

const sizeof = require("@g-lib/sizeof");

console.log(sizeof({ foo: "bar", bar: "baz" })); // 2
console.log(sizeof([1, 2, 3])); // 3
console.log(sizeof(new Map().set("foo", true))); // 1
console.log(sizeof(new Set().add("foo").add("bar"))); // 2
console.log(sizeof(2024)); // 4
console.log(sizeof(100e15)); // 18
console.log(sizeof(Symbol("foo"))); // 3
console.log(
  sizeof(function foo(a: any, b: any) {
    return a + b;
  })
); // 0
console.log(
  sizeof(function bar() {
    return "bar";
  })
); // 3
console.log(
  sizeof(
    new (function Foo(this: any) {
      this.name = "Foo";
      this.age = 30;
    } as any)()
  )
); // 2
console.log(
  sizeof(
    new (class Foo extends Map<any, any> {
      constructor(public name: string) {
        super();
      }
    })("")
  )
); // 0
console.log(
  sizeof(
    new (class Bar {
      constructor(public name: string) {}
    })("")
  )
); // 1

Contributing is always welcomed.

License MIT

About

A utility function that calculates the size or length of various input types.

Topics

Resources

License

Stars

Watchers

Forks