Answers for "javascript get string byte size"

0

js get string byte size

// JavaScript
(new Blob(['20€'])).size;		// 5

// Node js
Buffer.from('20€').length;	// 5
Posted by: Guest on July-17-2020
0

javascript get string byte size

getStringMemorySize = function( _string ) {
        "use strict";

        var codePoint
            , accum = 0
        ;

        for( var stringIndex = 0, endOfString = _string.length; stringIndex < endOfString; stringIndex++ ) {
            codePoint = _string.charCodeAt( stringIndex );

            if( codePoint < 0x100 ) {
                accum += 1;
                continue;
            }

            if( codePoint < 0x10000 ) {
                accum += 2;
                continue;
            }

            if( codePoint < 0x1000000 ) {
                accum += 3;
            } else {
                accum += 4;
            }
        }

        return accum * 2;
    }
Posted by: Guest on June-07-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language