A utility function that calculates the size or the length of various types.
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.