question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Maximum call stack size exceeded" for assert.fixture()

See original GitHub issue

One of my fixture tests fails because of a Maximum call stack size exceeded error.

OS: MacOS 11.4 Node: v14.15.3 uvu: 0.5.1

// json => readFileSync()
assert.fixture(JSON.stringify(analyze(css), null, 2), json)

This JSON is a 15.000+ lines file.

Workaround

To avoid adding the arr.map() callback to the call stack for every line (15.000+) I’m using a for-loop.

+ // https://github.com/lukeed/uvu/blob/1d753576ffab2dd28b309bb5c296a43d0800321c/src/assert.js#L4-L8
-function dedent(str) {
-	let arr = str.match(/^[ \t]*(?=\S)/gm);
-	let min = !!arr && Math.min(...arr.map(x => x.length));
-	return (!arr || !min) ? str : str.replace(new RegExp(`^[ \\t]{${min}}`, 'gm'), '');
-}
+function dedent(str) {
+	let arr = str.match(/^[ \t]*(?=\S)/gm);
+	if (!Array.isArray(arr) || arr.length === 0) {
+		return str
+	}
+	let min = -1
+       // Workaround for arr.map() to avoid add each map(fn) to call stack
+	for (let index = 0; index < arr.length; index++) {
+		const el = arr[index].length
+		if (el > min) {
+			min = el
+		}
+	}
+	return (!min) ? str : str.replace(new RegExp(`^[ \\t]{${min}}`, 'gm'), '');
+}

Happy to submit a PR if you think this is a suitable solution and worth fixing.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lukeedcommented, May 30, 2021

Ha, literally the one time I use newer syntax instead of a for-loop 🤦

PR would be great! How about this instead, which is only slightly modified & should still work:

function dedent(str) {
  let arr = str.match(/^[ \t]*(?=\S)/gm);
  let i = 0, min = 1/0, len = (arr||[]).length;
  for (; i < len; i++) min = Math.min(min, arr[i].length);
  return len && min ? str.replace(new RegExp(`^[ \\t]{${min}}`, 'gm'), '') : str;
}

Thanks!

0reactions
lukeedcommented, Jul 1, 2021

Ah ok cool, thanks! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Got "RangeError: Maximum call stack size exceeded" for many ...
The setup is the following: I have models called web, space and spaceview. Spaces and spaceviews are many-to-many and a web entity has...
Read more >
javascript - Maximum call stack size exceeded error
It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you...
Read more >
JavaScript RangeError: Maximum Call Stack Size Exceeded
The RangeError: Maximum call stack size exceeded is thrown when a function call is made that exceeds the call stack size. This can...
Read more >
Changelog - Cypress Documentation
The error Maximum call stack size exceeded will no longer throw when calling scrollIntoView on an element in the shadow dom. Fixes #7986....
Read more >
Avoid Maximum call stack size exceeded in JavaScript
Overview · Error Maximum call stack size exceeded is raised when calling a recursive function in a loop · This is expected –...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found