Nykakin / chompjs

Parsing JavaScript objects into Python data structures
MIT License
198 stars 11 forks source link

Optimize the code #19

Closed Nykakin closed 3 years ago

Nykakin commented 3 years ago

Experimenting with optimizing the code. Results so far... less impressive that I hoped:

Benchmark 1:

import base64
import itertools
import timeit

from _chompjs import parse

keys = (''.join(i) for i in itertools.combinations_with_replacement('abcdefghijklmnopqrstuvwxyz', 2))
keys_values = ('\'{}\': "{}",'.format(key, base64.b64encode('aa'.encode('ascii')).decode('utf-8')) for key in keys)
json_input = "{{{}}}".format(''.join(keys_values))

timeit.timeit('parse(json_input)', 'from __main__ import json_input, parse')

Benchmark 2:

import timeit

from _chompjs import parse

json_input = '[' * 400 + '2' + ']' * 400

timeit.timeit('parse(json_input)', 'from __main__ import json_input, parse')

Benchmark 3:

import timeit

from _chompjs import parse

json_data = str(list(range(500)))

timeit.timeit('parse(json_input)', 'from __main__ import json_input, parse')