I do this npm i -S json-size and then somewhere in my code.
import jsonSize from 'json-size'
console.log(jsonSize({foo: 'bar'})) //=> 13
This is what json-size is all about. github.com/bendrucker/json-size/blob/master/index…
'use strict'
var bytes = require('utf8-length')
module.exports = function jsonSize (value) {
return bytes(JSON.stringify(value))
}
json-size has a dependency of utf8-lenghth which is nothing more than this. github.com/substack/utf8-length/blob/master/index…
module.exports = function(s) {
return ~-encodeURI(s).split(/%..|./).length
}
We can do it all by ourself now.
const bytes = (s) => {
return ~-encodeURI(s).split(/%..|./).length
}
const jsonSize = (s) => {
return bytes(JSON.stringify(s))
}
console.log(jsonSize({foo: 'bar'})) //=> 13
What's this tilde operator? joezimjs.com/javascript/great-mystery-of-the-tilde