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.

Get wrong results when use regexp or Symbol as function arguments

See original GitHub issue

Because use JSON.stringify(args) in defaultCacheKey function, it will get wrong results when using regexp or Symbol as function arguments.

The reasons are:

console.log(JSON.stringify(/Sindre Sorhus/));
// => '{}'

console.log(JSON.stringify(/Elvin Peng/));
// => '{}'

console.log(JSON.stringify({foo: Symbol('Sindre Sorhus')}));
// => '{}'

console.log(JSON.stringify({foo: Symbol('Elvin Peng')}));
// =>'{}'

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fregantecommented, Apr 19, 2019

I just realized something, order matters:

JSON.stringify({a: 1, b: 1})
// "{\"a\":1,\"b\":1}"

JSON.stringify({b: 1, a: 1})
// "{\"b\":1,\"a\":1}"

This means that the cache is hit even less than we realize

1reaction
sindresorhuscommented, Aug 27, 2018

I’m open to suggestions on how to better handle this. A Symbol cannot really be properly stringified as it doesn’t have any value. Even if you give two symbols the same description, per definition, they are supposed to be unique, but there’s no way (as far as I know) for us to stringify them as unique values. A regex could be handled somehow though, since it can be stringified with .toString(). So maybe if instead of JSON.stringifying the whole arguments object, we stringify each item and also include its type and also specially handle regex? That would still not solve the Symbol case.

// @maxwellgerber @keithamus @LinusU

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regex for matching Functions and Capturing their Arguments
I have a function that searches the expression for math functions using Regex, retrieves the arguments, looks up the function name, and ...
Read more >
Regular Expressions :: Eloquent JavaScript
A regular expression is a type of object. It can be either constructed with the RegExp constructor or written as a literal value...
Read more >
RegExp - JavaScript - MDN Web Docs - Mozilla
Chrome Edge RegExp Full support. Chrome1. Toggle history Full support. Edge12. Toggle history @@match Full support. Chrome50. Toggle history Full support. Edge13. Toggle history @@matchAll Full...
Read more >
Methods of RegExp and String - The Modern JavaScript Tutorial
In this article we'll cover various methods that work with regexps in-depth. str.match(regexp). The method str.match(regexp) finds matches ...
Read more >
re — Regular expression operations — Python 3.11.1 ...
The solution is to use Python's raw string notation for regular expression patterns; backslashes are not handled in any special way in a...
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