Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import template from 'babel-template';
  2. import generate from 'babel-generator';
  3. import * as T from 'babel-types';
  4.  
  5. const nopt = require('nopt');
  6. const opts = {
  7. "compact": Boolean
  8. };
  9.  
  10. const assignTemplate = template(`
  11. var VAR = DATA;
  12. `);
  13.  
  14.  
  15. // create a program that initializes a variable of the form
  16. // var foo = { s0: { start: { line: 0, column: 0}, end: {line 1, column:0} }
  17. // etc. where the number of keys is controlled by the items arg
  18. function toProgram(items) {
  19. let obj = {};
  20. for (let i=0; i < items; i += 1) {
  21. const key = 's' + i;
  22. obj[key] = {
  23. start: {
  24. line: i,
  25. column: 0
  26. },
  27. end: {
  28. line: i + 1,
  29. column: 20
  30. }
  31. }
  32. }
  33. const node = T.valueToNode(obj);
  34. const v = assignTemplate({
  35. VAR: T.identifier("foo"),
  36. DATA: node
  37. });
  38. return {
  39. 'type': 'File',
  40. program: {
  41. 'type': 'Program',
  42. 'sourceType': 'script',
  43. body: [
  44. v
  45. ]
  46. }
  47. }
  48. }
  49.  
  50. const parsed = nopt(opts, null, process.argv, 2);
  51. const compact = parsed.compact;
  52.  
  53. const generateOptions = {
  54. compact: compact
  55. };
  56.  
  57. for (let i = 1; i < 15; i += 1) {
  58. const n = Math.pow(2, i);
  59. const prog = toProgram(n);
  60. const start = new Date().getTime();
  61. const codeMap = generate(prog, generateOptions, '');
  62. const end = new Date().getTime();
  63. if (i == 1) {
  64. console.log('Sample prog:', codeMap.code);
  65. }
  66. console.log('Items:', n, ', time:', (end - start));
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement