Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
masking performance
(version: 1)
Comparing performance of:
재귀 처리 vs JSON.stringify
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const emailRegex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi const phoneRegex = /([0-9]{2,3})[-]?([0-9]{3,4})[-]?([0-9]{4})/gi // 한글 2~5 자의 경우는 이름 뿐만 아니라 다른 경우도 있으므로 g 옵션을 제외 const koreanNameRegex = /^[가-힣]{2,5}$/ function isEmail(input) { return emailRegex.test(input) } function isPhoneNumber(input) { return phoneRegex.test(input) } function isKoreanName(input) { return koreanNameRegex.test(input) } function maskString(value) { const length = value.length if (length === 0) return value const mask = '*'.repeat(value.length - (length <= 2 ? 1 : 2)) /** * 길이가 2 이하인 경우 첫 글자만 노출되도록 처리 * 길이가 3 이상인 경우 처음과 마지막 글자는 노출되고 나머지는 마스킹 처리 */ return length <= 2 ? value[0] + mask : value[0] + mask + value[length - 1] } function maskEmail(value) { let result = value const matchString = value.match(emailRegex) matchString.forEach(email => { const [localPart, domainPart] = email.split('@') const maskedLocalPart = maskString(localPart) result = result.replace(email, `${maskedLocalPart}@${domainPart}`) }) return result } function maskPhoneNumber(value) { return value.replace(phoneRegex, '$1-****-$3') }
Tests:
재귀 처리
const data = [{ a: { name: "홍길동", b: { email: "abc@gmail.com", c: { phone: "01012341234" } }, } }] const data2 = { a: { name: "홍길동", b: { email: "abc@gmail.com", c: { phone: "01012341234" } }, } } function identifyAndMask(input) { if (isEmail(input)) { return maskEmail(input) } else if (isPhoneNumber(input)) { return maskPhoneNumber(input) } else if (isKoreanName(input)) { return maskString(input) } return input } function maskObjectStrings(obj) { if (typeof obj === 'object' && obj !== null) { if (Array.isArray(obj)) { // 배열인 경우 각 요소를 재귀적으로 처리 return obj.map(item => maskObjectStrings(item)) } else { // 객체인 경우 각 속성 값을 재귀적으로 처리 const maskedObj = {} for (const key in obj) { if (obj.hasOwnProperty(key)) { maskedObj[key] = maskObjectStrings(obj[key]) } } return maskedObj } } else if (typeof obj === 'string') { // 문자열인 경우 마스킹 return identifyAndMask(obj) } else { // 객체나 문자열이 아닌 경우 그대로 반환 return obj } } maskObjectStrings(data) maskObjectStrings(data2)
JSON.stringify
const data = [{ a: { name: "홍길동", b: { email: "abc@gmail.com", c: { phone: "01012341234" } }, } }] const data2 = { a: { name: "홍길동", b: { email: "abc@gmail.com", c: { phone: "01012341234" } }, } } function identifyAndMask(input) { let maskedInput = input if (isEmail(maskedInput)) { maskedInput = maskEmail(maskedInput) } if (isPhoneNumber(maskedInput)) { maskedInput = maskPhoneNumber(maskedInput) } if (isKoreanName(maskedInput)) { maskedInput = maskString(maskedInput) } return maskedInput } JSON.parse(identifyAndMask(JSON.stringify(data))) JSON.parse(identifyAndMask(JSON.stringify(data2)))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
재귀 처리
JSON.stringify
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Definition** The benchmark definition is a JavaScript function `maskObjectStrings` that takes an object as input and recursively masks sensitive information such as email addresses, phone numbers, and Korean names. **Options Compared** There are two different implementations of the `identifyAndMask` function: 1. **Recursive Implementation**: This implementation uses recursion to process each property of the object. It checks if a property is an email address, phone number, or Korean name, and if so, applies the corresponding masking function. 2. **Iterative Implementation**: This implementation uses iteration to process each property of the object. It checks if a property is an email address, phone number, or Korean name, and if so, applies the corresponding masking function. **Pros and Cons** 1. **Recursive Implementation** * Pros: + Easy to understand and implement + Can handle nested objects easily * Cons: + May cause stack overflow errors for very large objects due to recursive calls + Can be slower than iterative implementations due to the overhead of recursive function calls 2. **Iterative Implementation** * Pros: + Less likely to cause stack overflow errors + Can be faster than recursive implementations * Cons: + More complex to implement and understand + May require more memory to store intermediate results **Other Considerations** 1. **Use of JavaScript Primitives**: The benchmark uses JavaScript primitives such as `typeof` checks, which can affect the performance of the benchmark. 2. **Use of Regular Expressions**: The benchmark uses regular expressions to match email addresses and phone numbers, which can be computationally expensive. **Benchmark Result** The latest benchmark result shows that the recursive implementation is slower than the iterative implementation, with an average execution time of 324.39 executions per second compared to 198.02 executions per second for the iterative implementation. Overall, the benchmark suggests that the iterative implementation may be a better choice for masking sensitive information in JavaScript objects due to its potential for better performance and reduced risk of stack overflow errors.
Related benchmarks:
regex /g vs /
Regex vs Forloop Performance
parseFloat isNaN vs RegEx parseFloat
parseFloat isNaN vs RegEx parseFloat vs Number isNaN
s ds fzzxzx2
Comments
Confirm delete:
Do you really want to delete benchmark?