Comments (5)
Why? Half the web depends on it, and it's often more convenient for parsers than slice
or substring
. At the very least it shouldn't block any releases.
from peggy.
I'm not sure why it's more convenient than slice
or substring
? Can you say more?
from peggy.
To check that a
contains b
starting with a certain position (with correct handling of potential end of string) is usually done as a.substr(peg$pos, b.length) === b
. The other two methods take the end position instead of length, so with no substr
it should be computed as peg$pos + b.length
for no good reason.
As I understand, substr
will be there forever in JS implementations along with ordering of fields in objects, <marquee>
tag, with
operator and quirky behavior of sort
, because
- otherwise early websites would stop operating;
- there is no graceful degradation in case it ceases to exist;
- removing it from an implementation of a JS engine produces barely any profit;
- prototypes of globals can't really change in strict mode.
TLDR I don't see how following this deprecation might improve anything.
from peggy.
Not sure if this was just abandoned on the grounds that substr
will never be removed, but I did some investigation.
- the only place
substr
is used now is in generated code (and one reference fromdocs/vendor/codemirror
) - if I add a helper function,
peg$substr
at the same level aspeg$currPos
that just takes a length parameter, and returnsinput.slice(peg$currPos, peg$currPos + length)
, and use that instead ofinput.substr
everywhere, the generated parser is smaller (peg$substr(5)
vsinput.substr(peg$currPos, 5)
), and I can't measure a difference in performance in any of the parsers that I've generated (and I've tried with parser/input combos that take hundreds of ms). - I wrote a small test case that sets up something comparable to the parser, and just calls my new
substr
, orinput.substr
in a loop, and there I see a very small slowdown. Maybe as much as 10%, but there's so much noise it's hard to tell (on some runs my helper is actually faster, but its slower more often than not).
In summary, it seems like switching over is easy, there's a code size win (not a big deal), and maybe a very small performance penalty, but not measurable in real cases.
Here's the test case, in case I'm making a systematic error of some sort:
const fs = require("node:fs/promises");
function wrapper(input) {
let currPos = 0;
function substr(n) {
return input.slice(currPos, currPos + n);
}
function parseOld() {
currPos = 0;
while (input.substr(currPos, 5) !== "") {
currPos += 5;
}
}
function parseNew() {
currPos = 0;
while (substr(5) !== "") {
currPos += 5;
}
}
return [parseOld, parseNew];
}
fs.readFile("<a 20k text file>").then(
(data) => {
const [parseOld, parseNew] = wrapper(data.toString());
function test(testFn, which) {
const start = Date.now();
for (let i = 0; i < 10000; i++) {
testFn();
}
const end = Date.now();
console.log(`${which} Took ${end - start}ms`);
}
test(parseOld, "parseOld");
test(parseNew, "parseNew");
}
);
from peggy.
I agree that substr() will live forever, but there are ways of writing "modern" JavaScript. .slice() is mirrored by Array, which makes it more consistent. You can still use "var" and "==" all you want, but I wouldn't allow it in a recent PR. I think modernizing a code-base is a good goal to have. It's just confusing to have 3 functions that basically do the same thing.
from peggy.
Related Issues (20)
- Because version 3.0 already released, it's time to remove this option
- Release 3.02 HOT 1
- [docs] Parens create a pluck context HOT 7
- Run tests on es modules HOT 3
- Conversion of a pegjs file to BNF or vice versa ? HOT 2
- Type of `ParserBuildOptions` is wrong HOT 3
- [feature request] watch HOT 3
- doc example doesn't correspond to code example HOT 3
- Performance javascript grammar HOT 7
- test output format HOT 4
- Documentation improvements HOT 5
- use `expression1 / expression2 / ... / expressionn` throw `peg$SyntaxError: Expected end of input but "d" found.` HOT 5
- Playground syntax highlight grammar breaks when using template literals HOT 4
- tsconfig targets es5, but allows es2020 functions
- Documentation example for 'Negative assertion' gives an error in Playground
- `-o dist/my-parser.js` errors if `dist/` doesn't exist
- can i use peggy js with other languages like (zig-lang,c,c++ etc) HOT 1
- Invalid character range: \x80-\x10fffd HOT 3
- error location at diffrent place between online-editor and parsed-error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peggy.