0

Is there a more efficient way to strip functions from a large object than the following:

var objWithoutFunctions = JSON.parse(JSON.stringify(obj));

I want to avoid the stringify/parse combo if possible as the object might have cyclic references. And I'm generally curious if there is a way that performs better.

nils
  • 25,734
  • 5
  • 70
  • 79
  • have a look at this thread http://stackoverflow.com/questions/7914968/cloning-whats-the-fastest-alternative-to-json-parsejson-stringifyx – zoubida13 May 09 '16 at 14:48
  • 1
    The only alternative is to recursively iterate over the object and delete all properties / elements that are functions. You can profile the code to find out which one is faster. – Felix Kling May 09 '16 at 14:49
  • @zoubida13 thanks, I'm less interested in the cloning part though. – nils May 09 '16 at 14:54
  • @FelixKling thank you. TCO isn't implemented in any browser at the moment, right? – nils May 09 '16 at 15:07
  • Afaik no. The implementation wouldn't have to be recursive though (I used that term more to illustrate the problem). You can also implement a stack based solution. – Felix Kling May 09 '16 at 15:26

0 Answers0