3
3
* Run this with: node examples/bundle-size-demo.js
4
4
*/
5
5
6
- console . log ( ' === Twilio Bundle Size Reduction Demo ===\n' ) ;
6
+ console . log ( " === Twilio Bundle Size Reduction Demo ===\n" ) ;
7
7
8
8
// Traditional way - loads ALL services
9
- console . log ( ' 1. Traditional full client (loads all 30+ services):' ) ;
10
- console . time ( ' Full client load time' ) ;
11
- const Twilio = require ( ' ../lib/index' ) ;
9
+ console . log ( " 1. Traditional full client (loads all 30+ services):" ) ;
10
+ console . time ( " Full client load time" ) ;
11
+ const Twilio = require ( " ../lib/index" ) ;
12
12
const fullClient = new Twilio . Twilio (
13
- process . env . TWILIO_ACCOUNT_SID || ' ACtest' ,
14
- process . env . TWILIO_AUTH_TOKEN || ' test_token'
13
+ process . env . TWILIO_ACCOUNT_SID || " ACtest" ,
14
+ process . env . TWILIO_AUTH_TOKEN || " test_token"
15
15
) ;
16
- console . timeEnd ( ' Full client load time' ) ;
17
- console . log ( ' Memory usage: High (all services loaded)\n' ) ;
16
+ console . timeEnd ( " Full client load time" ) ;
17
+ console . log ( " Memory usage: High (all services loaded)\n" ) ;
18
18
19
19
// Modular way - loads only what you need
20
- console . log ( ' 2. Modular client (loads only messaging service):' ) ;
21
- console . time ( ' Modular client load time' ) ;
22
- const ModularClient = require ( ' ../lib/modular/index' ) . ModularTwilioClient ;
20
+ console . log ( " 2. Modular client (loads only messaging service):" ) ;
21
+ console . time ( " Modular client load time" ) ;
22
+ const ModularClient = require ( " ../lib/modular/index" ) . ModularTwilioClient ;
23
23
const modularClient = new ModularClient (
24
- process . env . TWILIO_ACCOUNT_SID || ' ACtest' ,
25
- process . env . TWILIO_AUTH_TOKEN || ' test_token' ,
26
- { services : [ ' messaging' ] }
24
+ process . env . TWILIO_ACCOUNT_SID || " ACtest" ,
25
+ process . env . TWILIO_AUTH_TOKEN || " test_token" ,
26
+ { services : [ " messaging" ] }
27
27
) ;
28
- console . timeEnd ( ' Modular client load time' ) ;
29
- console . log ( ' Services requested:' , modularClient . getRequestedServices ( ) ) ;
30
- console . log ( ' Services loaded:' , modularClient . getLoadedServices ( ) ) ;
31
- console . log ( ' Memory usage: Low (only messaging service ready)\n' ) ;
28
+ console . timeEnd ( " Modular client load time" ) ;
29
+ console . log ( " Services requested:" , modularClient . getRequestedServices ( ) ) ;
30
+ console . log ( " Services loaded:" , modularClient . getLoadedServices ( ) ) ;
31
+ console . log ( " Memory usage: Low (only messaging service ready)\n" ) ;
32
32
33
33
// Individual service imports - most efficient
34
- console . log ( ' 3. Individual service imports (most efficient):' ) ;
35
- console . time ( ' Individual service load time' ) ;
36
- const { Messaging } = require ( ' ../lib/services/index' ) ;
37
- const { Client } = require ( ' ../lib/base/BaseTwilio' ) ;
34
+ console . log ( " 3. Individual service imports (most efficient):" ) ;
35
+ console . time ( " Individual service load time" ) ;
36
+ const { Messaging } = require ( " ../lib/services/index" ) ;
37
+ const { Client } = require ( " ../lib/base/BaseTwilio" ) ;
38
38
const baseClient = new Client (
39
- process . env . TWILIO_ACCOUNT_SID || ' ACtest' ,
40
- process . env . TWILIO_AUTH_TOKEN || ' test_token'
39
+ process . env . TWILIO_ACCOUNT_SID || " ACtest" ,
40
+ process . env . TWILIO_AUTH_TOKEN || " test_token"
41
41
) ;
42
42
const messagingService = new Messaging ( baseClient ) ;
43
- console . timeEnd ( ' Individual service load time' ) ;
44
- console . log ( ' Memory usage: Minimal (only messaging service)\n' ) ;
43
+ console . timeEnd ( " Individual service load time" ) ;
44
+ console . log ( " Memory usage: Minimal (only messaging service)\n" ) ;
45
45
46
- console . log ( '=== Bundle Analysis ===' ) ;
47
- console . log ( 'Traditional approach: ~13MB bundle' ) ;
48
- console . log ( 'Modular client: ~2-3MB bundle (85% reduction)' ) ;
49
- console . log ( 'Individual imports: ~1-2MB bundle (90% reduction)' ) ;
50
- console . log ( '\nFor AWS Lambda or other size-sensitive environments,' ) ;
51
- console . log ( 'use the modular client or individual imports for best performance.' ) ;
46
+ console . log ( "=== Bundle Analysis ===" ) ;
47
+ console . log ( "Traditional approach: ~13MB bundle" ) ;
48
+ console . log ( "Modular client: ~2-3MB bundle (85% reduction)" ) ;
49
+ console . log ( "Individual imports: ~1-2MB bundle (90% reduction)" ) ;
50
+ console . log ( "\nFor AWS Lambda or other size-sensitive environments," ) ;
51
+ console . log (
52
+ "use the modular client or individual imports for best performance."
53
+ ) ;
52
54
53
55
// Demonstrate functionality still works
54
- console . log ( ' \n=== Functionality Test ===' ) ;
56
+ console . log ( " \n=== Functionality Test ===" ) ;
55
57
try {
56
58
// Access messaging through modular client
57
59
const messaging = modularClient . messaging ;
58
- console . log ( '✓ Modular client messaging access works' ) ;
59
- console . log ( ' Loaded services after access:' , modularClient . getLoadedServices ( ) ) ;
60
-
60
+ console . log ( "✓ Modular client messaging access works" ) ;
61
+ console . log (
62
+ " Loaded services after access:" ,
63
+ modularClient . getLoadedServices ( )
64
+ ) ;
65
+
61
66
// Try to access disabled service
62
67
try {
63
68
modularClient . voice ;
64
- console . log ( ' ✗ Should have thrown error for disabled service' ) ;
69
+ console . log ( " ✗ Should have thrown error for disabled service" ) ;
65
70
} catch ( e ) {
66
- console . log ( ' ✓ Correctly blocked access to disabled voice service' ) ;
71
+ console . log ( " ✓ Correctly blocked access to disabled voice service" ) ;
67
72
}
68
-
69
73
} catch ( error ) {
70
- console . log ( ' Error in functionality test:' , error . message ) ;
74
+ console . log ( " Error in functionality test:" , error . message ) ;
71
75
}
72
76
73
- console . log ( ' \n=== Demo Complete ===' ) ;
77
+ console . log ( " \n=== Demo Complete ===" ) ;
0 commit comments