4 ms·
Nearly an order of magnitude reduction. What do you think of that?
by amasad 5y ago
Nearly an order of magnitude reduction. What do you think of that?
- lhorie 5y agoThey probably mean that "hello world".length == 11 // bytes
- kklisura 5y agoIs it? I know there's no sizeof, but what would be sizeof(char) in JS? 1 byte?
- IggleSniggle 5y agoThe answer to this question is complicated. JavaScript char encoding is roughly UTF-16, which is 2-bytes, but the byte you read may have been part of a surrogate so, depending on your first one, you must read the next 2 bytes to complete your character. And of course, this basic explanation doesn’t really do justice to answering your question, because depending on your definition of what a “character” is, you may need to take into account ligatures etc.
- lhorie 5y agoNo need to overcomplicate though. None of this applies to the ascii range, and we're just talking about storage in disk in the first place.
- IggleSniggle 5y agoThe 2-bit part applies, if not the rest, and it’s not really “on disk” but rather “memory for data type,” right? Given the nature of the questions, I presumed they were interested in knowing “how does JavaScript load strings into memory, anyway?” And to answer that question, your rough heuristic should be “2 bytes per character” not 1, even for ascii range. That just leads to additional questions, though, because of the oddity of it. In order to achieve the ability to do Unicode, there’s a reserved set of values within that 2-bytes, to allow you to extend the encoding to reach Unicode. Back to the original measurement, for the string “hello world”, I believe a JavaScript `sizeof`, if it existed, would report 24 bytes (22 for the characters, and 2 (give or take) for either the NULL character or for a length header.
- lhorie 5y agoThe thread was originally about CRA vs Vite size on disk (or implicitly, if we're applying it to real world applications, network cost in CI job startup times). And like I said, surrogate pairs don't apply to ASCII. See this[0] for reference. Note how the first byte must fall within a certain range in order to signal being a surrogate pair. This range quite deliberately falls outside the ASCII range. This fact is taken advantage of by JS parsers to make parsing of ASCII substrings faster by special casing that range, since checking for a valid character in the entire unicode range is quite a bit more expensive[1]. IMHO nitpicking about memory consumption of the underlying data structure is a bit meaningless, since the spec doesn't actually enforce any guarantees about memory layout. An implementation can take more memory for pointer to prototype, to cache hash code/length, etc, and there are also considerations such as whether the underlying data structure is polymorphic or monomorphic due to JIT, whether the string is boxed/unboxed, whether it's implemented in terms of C strings vs slices, etc. Regardless, it doesn't change the fact that the octet sequence "hello world" takes 11 bytes in ASCII/UTF8 encoding (disregarding implementation metadata). [0] https://github.com/jquery/esprima/blob/0911ad869928fd218371b91eaae5d739f0c09aba/src/scanner.ts#L448 https://github.com/jquery/esprima/blob/0911ad869928fd218371b... [1] https://github.com/jquery/esprima/blob/0911ad869928fd218371b91eaae5d739f0c09aba/src/character.ts#L4-L8 https://github.com/jquery/esprima/blob/0911ad869928fd218371b...
- IggleSniggle 5y agoAll great points. Not trying to nitpick, just trying to satisfy curiosity.
- nine_k 5y agoMost of the build tooling gets installed along the way. The compiled JS is hundreds of bytes at most, usually because React creates a bunch of boilerplate for you (a basic CSS file, an application, a web worker, etc) which you may remove.
- XorNot 5y agoBut why would you? This is the silliest way to judge something ever: what practical web application is anyone routinely building with no CSS, web workers etc.
- Zarel 5y agoYou would because you're comparing it to a "Hello world" command-line program in another language which also doesn't have CSS, web workers, etc.
- lhorie 5y agoWell, that's part of the jab: that you ostensibly don't need build tooling for an interpreted language, and further, that you don't even need an interpreted language in the first place considering we're talking about a platform that can do UI out of the box via a declarative language. (Obviously, that's neither here or there when we're talking about baselines for SPAs)
- nine_k 5y agoES6 is a compile target. The actual languages are often Typescript and jsx, to say nothing of the Svelte compiler or Elm. You can write vanilla JS without all that, of course.