def remove_single_comments(content: str, markers: list[str]) -> str:
if not markers:
return content
if not content:
return ""
result = []
has_trailing_newline = content.endswith('\n')
for line in content.splitlines():
comment_start = None
for marker in markers:
pos = line.find(marker)
if pos != -1:
comment_start = pos if comment_start is None else min(comment_start, pos)
if comment_start is not None:
result.append(line[:comment_start])
else:
result.append(line)
result.append('\n')
if not has_trailing_newline and result: result.pop()
return ''.join(result)
if __name__ == "__main__":
import time
print("== Single line comment removal performance")
content_parts = []
for i in range(100000):
content_parts.append(f'let x{i} = {i}; // comment {i}\n')
content = ''.join(content_parts)
print(f"Input size: {len(content) / (1024 * 1024):.2f} MB")
start_time = time.time()
result = remove_single_comments(content, ["//", "<--"])
duration = (time.time() - start_time) * 1000
print(f"Output size: {len(result) / (1024 * 1024):.2f} MB")
print(f"Processed in {duration:.6f}ms")