test.js 989 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. var ARGS_RE = /^function\s*?\((.+)\)/,
  2. SCOPE_RE_STR = '\\.scope\\.[\\.A-Za-z0-9_$]+',
  3. noop = function () {}
  4. function test (fn) {
  5. var s = Date.now()
  6. var scope = {},
  7. str = fn.toString()
  8. console.log(Date.now() - s)
  9. var args = str.match(ARGS_RE)
  10. console.log(Date.now() - s)
  11. if (!args) return scope
  12. var argRE = new RegExp(args[1] + SCOPE_RE_STR, 'g'),
  13. matches = str.match(argRE)
  14. console.log(Date.now() - s)
  15. if (!matches) return scope
  16. var i = matches.length, j, path, key, level
  17. while (i--) {
  18. level = scope
  19. path = matches[i].slice(args[1].length + 7).split('.')
  20. j = 0
  21. while (j < path.length) {
  22. key = path[j]
  23. if (!level[key]) level[key] = noop
  24. level = level[key]
  25. j++
  26. }
  27. }
  28. console.log(scope)
  29. console.log(Date.now() - s)
  30. return scope
  31. }
  32. process.nextTick(function () {
  33. test(function (e) {
  34. return e.scope.a
  35. })
  36. })